Sort wp_query by meta value

2019-02-28 06:39发布

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条回答
虎瘦雄心在
2楼-- · 2019-02-28 07:10

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 );
查看更多
登录 后发表回答