将排序参数应用于新合并的查询

时间:2014-07-22 作者:amespower

我有两个带参数的查询,然后将它们合并成我的新查询-$wp\\u查询。现在,我想将参数应用到新查询中,以按meta\\u value\\u num显示合并的页面/帖子。如果我在$page\\u args数组中应用此排序,但不将其应用到组合数组,则此排序对该数组非常有效。此外,meta\\u value\\u num会在循环中打印出来,但根本不会对帖子/页面进行排序。我错过了什么?谢谢

$page_args = array(
    \'post_type\' => array( \'page\'),
    \'post__in\' => get_pageslug(" \'first_page\', \'second_page\' ")
);

$blog_args = array(
    \'post_type\'=> array(\'blog\'),
    \'showposts\' => 1,//latest
    \'offset\' => 0
);

$query_1 = new WP_Query( $page_args );//5
$query_2 = new WP_Query( $blog_args );//1

$args = array(
    \'meta_key\' => \'my_ordering\',
    \'orderby\' => \'meta_value meta_value_num\',
    \'order\' => \'ASC\'
);

// Create new empty query and populate it with the other two queries to combine
$wp_query = new WP_Query($args);
$wp_query->posts = array_merge( $query_1->posts, $query_2->posts ); 
$wp_query->post_count = count( $wp_query->posts ); //6

while ( $wp_query->have_posts() ) : $wp_query->the_post();
$box_ordering = get_post_meta($post->ID, \'my_ordering\', true);
echo $box_ordering . \'<br>\'; //outputs meta values for each page/post correctly
//but does not order the content by meta value
endwhile;

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

这是我要做的。这是经过轻度测试的。

它获取您需要的所有帖子的ID,然后构建一个查询,以正确的顺序获取帖子。

// get your page IDs, I assume this returns an array of integers
$page_ids = get_pageslug(" \'first_page\', \'second_page\' ");
// get your blog post ID
$blog_ids = new WP_Query( array(
    \'post_type\'=> array( \'blog\' ),
    \'posts_per_page\' => 1,
    \'fields\' => \'ids\', // just get the ID
    \'cache_results\' => false // because we\'re only getting the ID
) );

$my_wp_query_args = array(
    \'post_type\' => array( \'blog\', \'page\' ),
    \'post__in\' => array_merge( $page_ids, $blog_ids->posts ), // merge two arrays of IDs
    \'meta_key\' => \'my_ordering\',
    \'orderby\' => \'meta_value_num\', // meta_value_num is used *in place of* meta_value
    \'order\' => \'ASC\'
);

// NOTE: $wp_query is an internal WP object already so don\'t overwrite it.
$my_wp_query = new WP_Query( $my_wp_query_args );

while ( $my_wp_query->have_posts() ) : $my_wp_query->the_post();
    $box_ordering = get_post_meta($post->ID, \'my_ordering\', true);
    echo $box_ordering . \'<br>\'; //outputs meta values for each page/post correctly
endwhile;

结束

相关推荐

如何防止插件加载jQuery

我最近从wordpress下载了一个免费插件。我想从加载jQuery中删除插件,因为专家们知道它已经从wordpress头加载了。php文件。插件是testimonial-slider 如何编辑插件以防止其加载jQuery?我尝试了其他的方法,比如加入scrips,但没有成功。现在我正计划编辑这个插件,有人能指导我吗?谢谢