Sort wp_query by meta value

前端 未结 1 1725
温柔的废话
温柔的废话 2021-01-25 02:03

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 he

1条回答
  •  佛祖请我去吃肉
    2021-01-25 02:12

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

    0 讨论(0)
提交回复
热议问题