将搜索结果与其他WP_QUERY合并时,分页中断

时间:2013-11-01 作者:david

在“搜索结果”页面上(search.php) 我想执行额外的WP_Query 要保持搜索结果类型的特定顺序,例如,作者的搜索结果应始终显示在其他结果之前-

The first few pages should hold the author results, the other results should be shown on all the following pages only (not mixed on each page).

global $wp_query; //holds the results of the initial search query

$authorposts = $wp_query->posts;    
$query = new WP_Query( \'year=2012&monthnum=12&day=12\' );
$otherposts = $query->posts;

$wp_query->posts = array_merge( $authorposts, $otherposts  );
$wp_query->post_count = count( $wp_query->posts );

if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
  //show results loop...
分页行为出现了问题,而$authorposts 似乎正确分页,即正确的作者帖子页码和分页工作,只有那些存在的。但是$otherposts 另一方面,它们显示在每页的底部,即作者帖子的正下方。他们在每一页上都是一样的!此外,页面计数仅反映第一个查询的计数。

我还尝试与plus 操作员:

$wp_query->posts = $authorposts + $otherposts;
但在这种情况下$otherposts 根本不显示,页面计数保持不变(作者帖子的页面计数)。

我看到的所有使用两个单独查询的具有类似目标的示例看起来都是一样的-

So I\'m wondering if the my way of extending the original search query results with that additional WP_Query is sufficient or if I\'m missing some $wp_query fields to be populated (correctly)? What\'s the correct way doing this?

Update: 应该注意的是,我正在使用WP PageNavi插件分页,但这首先不应该相关,因为第一页已经呈现错误。它还使用查询对象初始化:wp_pagenavi( array( \'query\' => $wp_query ));

3 个回复
最合适的回答,由SO网友:gmazzap 整理而成

我认为唯一的解决方案是跳过sql分页,只通过php处理它。我的想法包括两个功能,一个连接pre_get_posts, 要筛选的第二个the_posts.

第一个函数做两件事:

取消设置搜索的分页值,这样所有帖子都将从SQL查询返回paged 全局变量中的值,以这种方式可以在稍后运行的第二个函数中使用此函数:

function filter_my_search_query( $query ) {
  if ( is_search() && $query->is_main_query() && ! is_admin() ) {
    global $the_original_paged;
    $the_original_paged = $query->get(\'paged\') ? $query->get(\'paged\') : 1;
    $query->set(\'paged\', NULL );
    $query->set(\'nopaging\', TRUE );
  }
}
add_action(\'pre_get_posts\', \'filter_my_search_query\', 1);
现在,搜索查询返回所有帖子,不分页,所需分页保存在全局变量中$the_original_paged.

所以我们可以过滤the_posts 合并想要的其他帖子,然后根据所需页面和每页帖子设置仅获取正确的帖子,最后重置paged 和其他$wp_query 让分页链接工作的属性:

function add_posts_to_search_query( $posts ) {
  global $wp_query, $the_original_paged;
  if ( ! is_main_query() || is_admin() || ! is_search() || is_null($the_original_paged) )
      return $posts;
  // the wanted posts per page here setted on general settings
  $perpage = get_option(\'posts_per_page\'); 
  remove_filter( \'the_posts\', \'add_posts_to_search_query\' );
  $new = new WP_Query( \'year=2012&monthnum=12&day=12&nopaging=1\' );
  $merged = array_merge( $posts, $new->posts );
  $wp_query->found_posts += $new->found_posts;
  // getting the right posts based on current page and posts per page
  $wp_query->posts = array_slice($merged, ( $perpage * ($the_original_paged-1) ), $perpage );
  // set the paged and other wp_query properties to the right value, to make pagination work
  $wp_query->set(\'paged\', $the_original_paged);
  $wp_query->post_count = count($wp_query->posts);
  $wp_query->max_num_pages = ceil( $wp_query->found_posts / $perpage ); 
  unset($the_original_paged); // clean up global variable
  return $wp_query->posts; 
}
add_filter(\'the_posts\', \'add_posts_to_search_query\');

SO网友:sam

过滤主查询以包括其他帖子。可以使用添加查询参数pre_get_posts 或使用修改SQLposts_requestposts_where.

滤器posts_resultsthe_posts 安排退回的岗位。

function filter_posts_where($where)
{
    $where .= \'OR …\' /* post_date is 2012-12-12 */;

    return $where;
}

function filter_the_posts($posts)
{
    usort($posts, \'compare_posts\');

    return $posts;
}

function compare_posts($a, $b)
{
    // compare "author" and dates?
    if     /* … */ return  1;
    elseif /* … */ return -1;
    else           return  0;
}

SO网友:david

我无法完全解决此问题涉及的所有问题,但我将当前状态发布在此处以供参考:

function SQ_the_posts($posts, $q = false) {
    if( is_search() && is_main_query() ){

            global $wp_query;

            $authorposts = $wp_query->posts;
            $paged = get_query_var(\'paged\');

            remove_filter( \'the_posts\', \'SQ_the_posts\' );
            $query = new WP_Query( \'year=2012&monthnum=12&day=12\' );
            add_filter( \'the_posts\', \'SQ_the_posts\' );

            $otherposts = $query->posts;
            $mergedPosts = array_merge( $authorposts, $otherposts  );

            $wp_query->found_posts += $query->found_posts;
            $initialMaxNumPages = $wp_query->max_num_pages;
            if ($paged > $initialMaxNumPages) {
                    $wp_query->posts = $otherposts;
                    $wp_query->post_count = $query->post_count;
                    $posts = $otherposts;
            }

            $wp_query->max_num_pages = ($wp_query->found_posts > 0) ? $wp_query->found_posts / 10 : 0;
            $wp_query->max_num_pages += ($wp_query->found_posts % 10) ? 1 : 0;          
    }
    return $posts;
}
add_filter( \'the_posts\', \'SQ_the_posts\' );

What it does:

<当max_num_pages 超出第一个查询的

What doesn\'t work:

<例如,如果第一个查询(作者搜索查询)的结果为9页,而第二个查询返回5页,则分页将正确显示14页。但当我单击第10页时,分页显示“第10页,共5页”,因为第一个查询的9页在该页上不再可用。有没有办法解决这个问题
结束

相关推荐

Display Search Result Count

到目前为止,我一直在使用下面的代码来获取搜索结果的数量,并显示该计数。<?php /* Search Count */ $allsearch =& new WP_Query(\"s=$s&showposts=-1\"); $count = $allsearch->post_count; echo $count . \' \'; wp_reset_query(); ?> 但这似乎不是有效的代码。显示以下错误:不推荐:不推荐通过引用分配new的返回值请任何人建议我获