Sort wp_query by meta value

匿名 (未验证) 提交于 2019-12-03 02:38:01

问题:

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 :::

My Code

$args = array(     "post_type" => "portfolio",     "meta_key" => "authors_name",     "orderby" => "meta_value",     "order" => "ASC" );  $custom_query = new WP_Query( $args ); 

This Also Does Not Work

$args = array(     "post_type" => "portfolio",     "meta_key" => "authors_name",         'meta_query' => array(             array(                 'key' => 'authors_name',             ),             ),             'orderby' => 'meta_value',             'order' => 'ASC', ); 

回答1:

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


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!