web-dev-qa-db-ja.com

WordPress Paginate $ wpdb-> get_results

複雑なマルチサイト設定のため、2つのブログからの投稿を組み合わせたクエリがあり、 結果にページ番号を付けたい 。私はどんな助けにも感謝しています。クエリを投稿しました。

            $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>
            ';

            }
7
uknowit2

更新

私はこれをテストし、それは私のサイト上で動作します。いくつかのこと:

  • 私の$queryをあなたのものに置き換えてください
  • global $wpdb(グローバル変数に関するあなたのコメントによる)
  • 特に指示がない場合、get_results()はオブジェクトを返します(2番目のパラメータは戻り値の型です)
  • 私はこれをプラグインに入れましたが、あなたはコードを抽出してあなたのテーマに入れることも、単に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
));

参考文献:

// This worked great for me so much thanks! I just adapted for what I needed. Right in template file, Sweet!
global $wpdb;
// QUERY HERE TO COUNT TOTAL RECORDS FOR PAGINATION $total = $wpdb->get_var("SELECT COUNT(*)
$post_per_page = 10;
$page = isset( $_GET['cpage'] ) ? abs( (int) $_GET['cpage'] ) : 1;
$offset = ( $page * $post_per_page ) - $post_per_page;

// QUERY HERE TO GET OUR RESULTS $results = $wpdb->get_results

// PHP FOR EACH LOOP HERE TO DISPLAY OUR RESULTS
// END OUR FOR EACH LOOP

// PAGINATION HERE IN Nice BOOTSTRAP STYLES
<?php 
echo '<div class="pagination">';
echo paginate_links( array(
'base' => add_query_arg( 'cpage', '%#%' ),
'format' => '',
'prev_text' => __('&laquo;'),
'next_text' => __('&raquo;'),
'total' => ceil($total / $post_per_page),
'current' => $page,
'type' => 'list'
));
echo '</div>';
?>
1
cmocha