带有元键的POST类型的自定义分页搜索

时间:2017-12-05 作者:Manmeet Khurana

我是WordPress新手,尝试基于自定义查询设置分页,使用query从元键自定义字段和产品结果中找到结果。

但分页查询结果基于$wp\\u query时出现了问题。

如何将自定义查询的值设置为$wp\\u查询

    //$s=\'GPO2\'
    //query for meta search 
    // searching  the result from the meta field 
     $q1 = array(
        \'fields\' => \'ids\',
        \'posts_per_page\' => -1,
        \'post_type\' => \'products\',
        \'meta_query\' => array(
            \'relation\' => \'OR\',
            array(
                \'key\' => \'product_sku\',
                \'value\' => sanitize_text_field( $s ),
                \'compare\' => \'LIKE\'
            ),
            array(
                \'key\' => \'  
    product_color_0_color\',
                \'value\' => sanitize_text_field($s),
                \'compare\' => \'LIKE\'
            )
        )
    );
            $loop = new WP_Query( $q1 );

    // search the result from main table         

     $q2 = &new WP_Query("s=$s&showposts=-1&post_type=products&fields=ids"); 

    //here by using these queries  found the unique ids

    if(!empty($loop->posts) && !empty($q2->posts))
    {
      $unique = array_unique( array_merge( $loop->posts, $q2->posts ) );  
    }
    elseif(!empty($loop->posts) && empty($q2->posts))
    {

      $unique=$loop->posts;
    }else{
      $unique=$q2->posts;
    }



    if ( get_query_var(\'paged\') ) {
        $paged = get_query_var(\'paged\');
    } elseif ( get_query_var(\'page\') ) { // \'page\' is used instead of \'paged\' on Static Front Page
        $paged = get_query_var(\'page\');
    } else {
        $paged = 1;
    }


    $posts= array(
        \'post_type\' => \'products\',
        \'post__in\' => $unique,
          \'paged\' => $paged,
        \'post_status\' => \'publish\',
        \'posts_per_page\' => 10
    );

    $allsearch= &new WP_Query( $posts );
    the_posts_pagination( array(
            \'format\'             => \'page/%#%/?s=\'.$msgSearch.\'\',
            \'prev_text\' => \'<span class="screen-reader-text">\' . __( \'Previous page\', \'twentyseventeen\' ) . \'</span>\',
                    \'next_text\' => \'<span class="screen-reader-text">\' . __( \'Next page\', \'twentyseventeen\' ) . \'</span>\',
                    \'before_page_number\' => \'<span class="meta-nav screen-reader-text">\' . __( \'Page\', \'twentyseventeen\' ) . \' </span>\',
            \'current\' => max( 1, get_query_var(\'paged\') ),
            \'total\' => $allsearch->max_num_pages,
            \'rewrite\' => false
                ) );

1 个回复
SO网友:Bruno Cantuaria

这个the_posts_pagination 可能依赖于主查询。您将需要“破解”查询全局var或简单地替换主查询(如果以后不使用它)。

因此,替换主查询:

//$allsearch= &new WP_Query( $posts );
query_posts($posts);
或替换主查询变量

$allsearch= &new WP_Query( $posts );
$backup = $GLOBALS[\'wp_query\'];
$GLOBALS[\'wp_query\'] = $allsearch;

the_posts_pagination();

$GLOBALS[\'wp_query\'] = $backup;
另外,我注意到您使用了一个名为$posts的变量。我已经在查询中使用全局变量名时遇到了一些问题,所以我不建议使用名为$posts的变量

结束

相关推荐

Custom taxonomy pagination

我正在为自定义分类法存档页创建自定义编号分页。因此,我在显示分页、显示正确的链接等方面没有问题。但是,当我单击第2页上方的链接时(例如第3页,如:my-website.com/page/3/?my_category=some-term我得到404-页面不存在。该类别包含60多篇帖子,应该有8页,但只有第一页和第二页正确显示my-website.com/?my_category=some-term和my-website.com/page/2/?my_category=some-term但以上所有的都是404