Kevin Daniel
Kevin Daniel

Reputation: 431

Wordpress sort by meta_value not working with meta_query

I have a template that pulls a custom post type for properties and sorts them by state. The script works properly until you add in a meta_query for the agent. It pulls the correct properties, but it doesn't sort them by state with the meta_query present. Here's the query:

$qry = array(
        'post_type' => array( 'practices-tpsg' ),
        'meta_key'  => 'wpcf-practice-state',
        'orderby'   => 'meta_value',
        'order'     => 'asc',
        'showposts' => 18,
        'paged'     =>$paged
    );

if($_GET['agent'])
{
    $qry['meta_query'] = array(
        'relation' => 'OR',
        array(
            'key' => 'wpcf-agent',
            'value' => $_GET['agent']
        ),
        array(
            'key' => 'wpcf-agent2',
            'value' => $_GET['agent']
        ),
    );
}

Does anyone know why it would stop sorting properly after the meta_query is added to the query?

Here's a link to the page: http://www.totalpracticesolutionsgroup.com/practices-for-sale/ The sort order works properly in the default view, which allows the state name headers above the listings. If you select an agent from the drop down in the top right, the correct properties are pulled, but they are no longer sorted by state. You can also see the output of the query by adding &debug=1 to the end of the url.

Upvotes: 1

Views: 671

Answers (1)

Prince Singh
Prince Singh

Reputation: 5183

replace 'orderby' => 'meta_value' with 'orderby' => 'meta_value_num

Upvotes: 1

Related Questions