Mick D
Mick D

Reputation: 172

Multiple meta_key in WP_Query

I want to sort my content. I'd like to sort it on two custom meta_keys (the status and the deadline).

So far I've got this:

$mypost = array( 
    'post_type' => 'customposttype', 
    'order' => $_GET['order'], //ASC
    'orderby' => $_GET['orderby'], //meta_value
    'meta_key' => $_GET['meta_key'], //status
    'meta_value' => $_GET['meta_value'] //live
);  

$loop = new WP_Query( $mypost );

With the following url I can achieve this query:

<a href="<?php echo add_query_arg(array ('orderby' => 'meta_value', 'meta_key' => 'status', 'meta_value' => 'live', 'order' => 'ASC'));?>">Show status=live results</a>

The bottomline is: I also want to sort them on 'status=live' and 'deadline'.

The deadline is also a 'meta_key'. Anyone out here know what my query should be like!?

Upvotes: 2

Views: 6788

Answers (1)

jogesh_pi
jogesh_pi

Reputation: 9782

I don't know hows you fetching the result through link, Take a look on the below code example to prepare a query args for meta key-values. I have not tested it before, Please let me know if it works for you.

$mypost = array( 
    'post_type'  => 'custom-post-type', 
    'order'      => 'ASC', //ASC
    'orderby'    => 'meta_value', //meta_value
    'meta_query' => array(
            'relation' => 'OR',
    array(
        'key'   => 'status',
        'value' => 'live'
    ),
    array(
        'key'   => 'status',
        'value' => 'deadline'
    )
)

);  

$loop = new WP_Query( $mypost );

take a look on the given link that might help you more WP_Query

Upvotes: 2

Related Questions