按meta值对wp_query进行排序 [英] Sort wp_query by meta value
本文介绍了按meta值对wp_query进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个自定义帖子类型,即投资组合,我需要能够通过meta值对它进行排序,meta值是作者的名字:::我想尝试几个示例,但没有用到:::任何帮助将不胜感激: ::
I have a custom post type namely portfolio, I need to be able to sort this by meta value which is the authors name ::: I've bee trying several examples but none work ::: Any help would be appreciated :::
我的代码
$args = array(
"post_type" => "portfolio",
"meta_key" => "authors_name",
"orderby" => "meta_value",
"order" => "ASC"
);
$custom_query = new WP_Query( $args );
这也不起作用
$args = array(
"post_type" => "portfolio",
"meta_key" => "authors_name",
'meta_query' => array(
array(
'key' => 'authors_name',
),
),
'orderby' => 'meta_value',
'order' => 'ASC',
);
推荐答案
我能够使用add_filter('pre_get_posts':::来解决此问题:本质上,这就是我的脚本现在看起来像:::
I was able to sort this issue with add_filter('pre_get_posts' ::: In essence this is what my script now looks like :::
function laudes_order( $wp_query ) {
$wp_query->set('meta_key', 'authors_name');
$wp_query->set('orderby', 'meta_value');
$wp_query->set('order', 'DESC');
}
add_filter('pre_get_posts', 'laudes_order');
$args = array(
"post_type" => "portfolio",
);
$custom_query = new WP_Query( $args );
这篇关于按meta值对wp_query进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文