我是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
) );