我读了很多书,尝试了很多,但都没有得到我想要的:我有自定义的帖子类型,我想在DESC中排序,按其元值排序:
完整查询及其参数:
wp_reset_query();
$args = array(
\'meta_key\' => \'completeness\',
\'order_by\' => \'meta_value_num\',
\'posts_per_page\' => -1,
\'order\' => \'DESC\',
\'post_type\' => \'datensatz\',
\'post_status\' => \'draft\'
);
$the_query = new WP_Query( $args );
if ( $the_query -> have_posts() ):
while ( $the_query -> have_posts()) :
$the_query -> the_post();
global $post;
echo get_the_title().\':\'.get_post_meta( $post->ID, "completeness", TRUE ).\'<br/>\';
endwhile;
endif;
wp_reset_query();
此查询不起作用。任何帮助都将不胜感激。
我刚刚查看了$wpdb->last_query
得到这个:
"SELECT post_id, meta_key, meta_value FROM wp_postmeta WHERE post_id IN (206,200,199,198,197,[..]) ORDER BY meta_id ASC"
所以我的WP查询肯定是错误的。。。
最合适的回答,由SO网友:Robert hue 整理而成
您的查询看起来不错,但有几个问题。而不是您的代码(来自wp_reset_query();
到wp_reset_query();
) 使用以下代码,让我们知道输出是什么。
$new_args = array(
\'post_type\' => \'datensatz\',
\'post_status\' => \'draft\',
\'posts_per_page\' => -1,
\'meta_key\' => \'completeness\',
\'orderby\' => \'meta_value_num\',
\'order\' => \'DESC\',
);
$my_new_query = new WP_Query( $new_args );
var_dump( $my_new_query->request );
EDIT
现在试试这个。这也应该起作用。
$new_args = array(
\'post_type\' => \'datensatz\',
\'post_status\' => \'draft\',
\'posts_per_page\' => -1,
\'meta_key\' => \'completeness\',
\'orderby\' => \'meta_value_num\',
\'order\' => \'DESC\',
);
$my_new_query = new WP_Query( $new_args );
if ( $my_new_query -> have_posts() ) :
while ( $my_new_query -> have_posts()) : $my_new_query -> the_post();
echo get_the_title() . \': \' . get_post_meta( $post->ID, "completeness", TRUE ). \'<br/>\';
endwhile;
endif;
wp_reset_postdata();