问题描述

由于复杂的多站点配置,我有一个查询结合了两个博客的帖子,我想分页结果。我很感谢任何帮助。我已经发布了我的查询。

            $latestposts = $wpdb->get_results(

            "
            (SELECT * FROM net_5_posts
            INNER JOIN net_5_term_relationships ON net_5_posts.ID=net_5_term_relationships.object_id
            WHERE post_type = 'post'
            AND post_status = 'publish'
            AND term_taxonomy_id = '151'
            )

            UNION ALL

            (SELECT * FROM net_7_posts
            INNER JOIN net_7_term_relationships ON net_7_posts.ID=net_7_term_relationships.object_id
            WHERE post_type = 'post'
            AND post_status = 'publish'
            AND term_taxonomy_id = '20'
            )

            ORDER BY post_date
            DESC LIMIT 5",'ARRAY_A');

            foreach ($latestposts as $latestpost) {

            $da_id = $latestpost['ID'];
            $da_title = $latestpost['post_title'];
            $da_content = strip_tags($latestpost['post_content']);
            $da_content = limit_words($da_content,55);
            $da_link = $latestpost['guid'];
            $da_date = $latestpost['post_date'];
            $da_date = date('F j, Y', strtotime($da_date));

            echo '
            <div class="ldapost">
            <h2 class="lheader"><a href="'.$da_link.'">'.$da_title.'</a></h2>
            <span class="ldate">'.$da_date.'</span>
            <span class="lcontent">'.$da_content.'…</span><br>
            <a class="button btnright" href="'.$da_link.'">Continue Reading</a>
            </div>
            ';

            }

最佳解决方案

Update

我已经测试了,它在我的网站上工作。一些东西:

  • 用我的 $query 替换

  • global $wpdb(根据您对全局变量的评论),因为它超出了范围!

  • get_results()在没有通知的情况下返回一个对象 (第二个参数是返回类型)

  • 我把它放在一个插件中,但是你可以提取代码并将其放在主题中,或者将其放在 functions.php 中。

这是功能:

function test_function() {

    global $wpdb;

    $query = "
        (SELECT * FROM wp_18_posts
        INNER JOIN wp_18_term_relationships ON wp_18_posts.ID=wp_18_term_relationships.object_id
        WHERE post_type = 'post'
        AND post_status = 'publish'
        AND term_taxonomy_id = '2')

        UNION ALL

        (SELECT * FROM wp_17_posts
        INNER JOIN wp_17_term_relationships ON wp_17_posts.ID=wp_17_term_relationships.object_id
        WHERE post_type = 'post'
        AND post_status = 'publish'
        AND term_taxonomy_id = '2')";

    $total_query = "SELECT COUNT(1) FROM (${query}) AS combined_table";
    $total = $wpdb->get_var( $total_query );
    $items_per_page = 1;
    $page = isset( $_GET['cpage'] ) ? abs( (int) $_GET['cpage'] ) : 1;
    $offset = ( $page * $items_per_page ) - $items_per_page;
    $latestposts = $wpdb->get_results( $query . " ORDER BY post_date LIMIT ${offset}, ${items_per_page}" );

    foreach ($latestposts as $latestpost) {
        $da_id = $latestpost->ID;
        $da_title = $latestpost->post_title;
        $da_content = strip_tags($latestpost->post_content);
        $da_content = wp_trim_words($da_content, 55);
        $da_link = $latestpost->guid;
        $da_date = $latestpost->post_date;
        $da_date = date('F j, Y', strtotime($da_date));

        echo '
        <div class="ldapost">
        <h2 class="lheader"><a href="'.$da_link.'">'.$da_title.'</a></h2>
        <span class="ldate">'.$da_date.'</span>
        <span class="lcontent">'.$da_content.'…</span><br>
        <a class="button btnright" href="'.$da_link.'">Continue Reading</a>
        </div>
        ';
    }

    echo paginate_links( array(
        'base' => add_query_arg( 'cpage', '%#%' ),
        'format' => '',
        'prev_text' => __('&laquo;'),
        'next_text' => __('&raquo;'),
        'total' => ceil($total / $items_per_page),
        'current' => $page
    ));
}

原文

paginate_links 功能独立于您的查询。给定一些参数,如项目总数和当前页面,它可以提供您要查找的分页。所以你需要计算:

  1. 项目总数

  2. 当前页码,1 个

  3. mysql 限制语句的偏移量。

我在想这样的事情 (未经测试,对不起!):

$query = "
    (SELECT * FROM net_5_posts
    INNER JOIN net_5_term_relationships ON net_5_posts.ID=net_5_term_relationships.object_id
    WHERE post_type = 'post'
    AND post_status = 'publish'
    AND term_taxonomy_id = '151'
    )

    UNION ALL

    (SELECT * FROM net_7_posts
    INNER JOIN net_7_term_relationships ON net_7_posts.ID=net_7_term_relationships.object_id
    WHERE post_type = 'post'
    AND post_status = 'publish'
    AND term_taxonomy_id = '20'
    )";

$total = $wpdb->get_var( "SELECT COUNT(1) FROM (${query}) AS combined_table" );
$items_per_page = 5;
$page = isset( $_GET['cpage'] ) ? abs( (int) $_GET['cpage'] ) : 1;
$offset = ( $page * $items_per_page ) - $items_per_page;
$latestposts = $wpdb->get_results( $query . " ORDER BY post_date LIMIT ${offset}, ${items_per_page}" );

foreach ($latestposts as $latestpost) {
    // Your code here ...
}

echo paginate_links( array(
    'base' => add_query_arg( 'cpage', '%#%' ),
    'format' => '',
    'prev_text' => __('&laquo;'),
    'next_text' => __('&raquo;'),
    'total' => ceil($total / $items_per_page),
    'current' => $page
));

参考文献:

参考文献

注:本文内容整合自 Google/Baidu/Bing 辅助翻译的英文资料结果。如果您对结果不满意,可以加入我们改善翻译效果:薇晓朵技术论坛。