使用自定义SQL查询进行分页


9

我有自己的SQL字符串,可以选择带有特定WHERE子句的Custom Post Type帖子。我使用了offset和limit根据所显示的页面返回适当的帖子。很好

现在,我想提出previous_posts_link()next_posts_link()功能的工作。它们都被称为get_posts_nav_link从哪使用global $wp_query

有没有一种方法可以重新分配global $wp_query我的SQL字符串或$wpdb->get_results结果或其他任何内容?因此,本机previous_posts_link()next_posts_link()WP函数将起作用。

如果没有,我该如何重现上一个和下一个后链接功能?

我将不胜感激任何帮助和建议!我完全坚持了这一点。
谢谢 :)

注意:我刚刚注意到previous_posts_link()在所有页面上都可以正常工作,但是no idea why在这种情况下,为什么next_posts_link不起作用:S

这是代码:

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$post_per_page = intval(get_query_var('posts_per_page'));
$offset = ($paged - 1)*$post_per_page;

$sql = "
SELECT SQL_CALC_FOUND_ROWS  wp_posts.*, wp_postmeta.* 
FROM wp_posts 
INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id)
INNER JOIN wp_postmeta AS mt1 ON (wp_posts.ID = mt1.post_id) 
WHERE 1=1  
    AND wp_posts.post_type = 'movie' 
    AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private') 
    AND ((wp_postmeta.meta_key = '_expiry_date' AND CAST(wp_postmeta.meta_value AS DATE) >= '".$current_date."') 
        OR (mt1.meta_key = '_expiry_date' AND CAST(mt1.meta_value AS CHAR) = ''))
GROUP BY wp_posts.ID 
ORDER BY wp_posts.post_date DESC
LIMIT ".$offset.", ".$post_per_page;

$movies_all_current = $wpdb->get_results( $sql, OBJECT);

if($movies_all_current) {
global $post;

//loop
foreach( $movies_all_current as $key=>$post ) {
    setup_postdata($post);
    //display each post
    //...
} //end foreach ?>

    //navigation
<div class="navigation">
    <div class="previous panel"><?php previous_posts_link('&laquo; newer') ?></div>
    <div class="next panel"><?php next_posts_link('older &raquo;') ?></div>
</div>
}

Answers:


16

好吧,我到了最后。我不能使用WP_Query类,因为我确实需要拥有自己的大型复杂SQL。我最终得到的是:

functions.php我的自定义SQL和逻辑中,用于计算WP分页逻辑所需的值:

function vacancies_current( ){
    global $wpdb, $paged, $max_num_pages, $current_date;

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $post_per_page = intval(get_query_var('posts_per_page'));
    $offset = ($paged - 1)*$post_per_page;

    /* Custom sql here. I left out the important bits and deleted the body 
     as it will be specific when you have your own. */
    $sql = "
        SELECT SQL_CALC_FOUND_ROWS  {$wpdb->posts}.*
        FROM {$wpdb->posts}
        ....
        GROUP BY {$wpdb->posts}.ID 
        ORDER BY {$wpdb->posts}.post_date DESC
        LIMIT ".$offset.", ".$post_per_page."; ";   

    $sql_result = $wpdb->get_results( $sql, OBJECT);

    /* Determine the total of results found to calculate the max_num_pages
     for next_posts_link navigation */
    $sql_posts_total = $wpdb->get_var( "SELECT FOUND_ROWS();" );
    $max_num_pages = ceil($sql_posts_total / $post_per_page);

    return $sql_result;
}

然后在我的模板文件中有:

<?php 
    $vacancies_current = vacancies_current();
    /*followed by a standart loop to display your results */ 
 ?>
<div class="navigation">
    <div class="previous panel"><?php previous_posts_link('&laquo; previous vacancies',$max_num_pages) ?></div>
    <div class="next panel"><?php next_posts_link('more vacancies &raquo;',$max_num_pages) ?></div>
</div>

诀窍是在供应previous_posts_link()next_posts_link$max_num_pages正确地计算它的价值和明显。

这很好。希望它可以帮助某人:)

大沙


+1不错的工作。在研究我对stackoverflow.com/questions/16057059/…的答案时,我遇到了这个问题(并表示衷心的感谢)。我想知道,如果你知道的一种方式来使用这样的自定义SQL语句,但在pre_get_posts()按照行动codex.wordpress.org/...?根据wordpress.org/support/topic/…,我发现此解决方案易受最后一页上的404 攻击。您是如何克服这一难题的?
Sepster


1

扩展有关Anu的答案。无需依赖自定义的SQL查询,您可以使用WP_Query类,并让WordPress处理所有繁重的SQL任务。这肯定会解决您的导航问题。

在_expiry_date meta_key中查询电影帖子类型的示例:

$today = getdate();
$args = array(
    'post_type' => 'movie',
    'meta_query' => array(
            'meta_key' => '_expiry_date',
            'meta_value' => $today,
            'meta_compare' => '< '
                    ),
    'posts_per_page' => -1,
     'order'    => 'DESC'
    );

    $movie_query = new WP_Query( $args );

    while ( $movie_query->have_posts() ) : $movie_query->the_post(); 
    // Do stuff
   endwhile; ?>

 <div class="navigation">
<div class="previous panel"><?php previous_posts_link('&laquo; newer') ?></div>
<div class="next panel"><?php next_posts_link('older &raquo;') ?></div>
</div>

谢谢您的回答,但是,我不能依赖WP_Query类,因为我需要构建自己的自定义SQL。我到了那里,如果有兴趣的话,请看我的回复:)
dashaluna 2011年

-2
<?php

global $wpdb, $paged;
query_posts($query_string . '&posts_per_page=9');
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$author = isset($_GET['author_name']) ? get_userdatabylogin($author_name) : get_userdata(intval($author));

query_posts($query_string . '&posts_per_page=9');

$args = array(
'post_type' => 'post',
'meta_query' => array(
        'meta_key' => 'autor',
    'post_status' => 'publish',
        'meta_value' => $author->id,
            ),
'paged' => $paged,
'posts_per_page' => 9,
'order'    => 'DESC'
);

$postsQuery = new WP_Query( $args );

?> 

模板:

<h1lánky od <?php echo $author->display_name; ?></h1>
        <ul class="thumbnails">

            <?php while ( $postsQuery->have_posts() ) : $postsQuery->the_post();  ?>
                <li class="span3">
                <div class="thumbnail">
                    <a href="<?php the_permalink(); ?>">
                    <?php the_post_thumbnail(array(260, 259)); ?>
                    </a>
                    <?php
                    $class = '';
                    if (in_category('fashion')) {
                    $class = "link-fashion";
                    } else if (in_category('beauty')) {
                    $class = "link-beauty";
                    } else if (in_category('gourmet')) {
                    $class = "link-gourmet";
                    } else if (in_category('lifestyle')) {
                    $class = "link-lifestyle";
                    } else if (in_category('about-us')) {
                    $class = "link-about";
                    }
                    ?>
                    <a href="<?php the_permalink(); ?>">
                    <h2 class="<?=  $class ?>">
                        <span></span>
                        <?php
                        // short_title('...', 25); 
                        echo get_the_title();
                        ?>
                    </h2>
                    </a>
                    <?php the_excerpt(); ?>
                    <hr>
                </div>
                </li>
            <?php endwhile; ?>

        </ul>
        <?php wp_pagenavi(); ?>

2
请在代码中加上说明,为什么还要运行两个查询,一个查询query_posts与一个查询WP_Query
Pieter Goosen
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.