WordPress query by multiple metaboxes and order by date

So here is my query:

$args = array( 
    'post_type' => 'Event',
    'posts_per_page' => 1000,
    'meta_key' => 'event_informations_show_on_the_homepage',
    'meta_value' => 'Show on the homepage',
    'meta_compare' => '==',
    'meta_key' => 'event_informations_date',
    'orderby' => 'meta_value_num',
    'order' => 'ASC'
);

$loop = new WP_Query( $args );

I want to select all posts that have the metabox event_informations_show_on_the_homepage and the value of the metabox event_informations_show_on_the_homepage and order by the date metabox which is stored as a timestamp and is called event_informations_date.

Read More

What am I doing wrong?

Related posts

Leave a Reply

1 comment

  1. Hopefully I’m not barking up the wrong tree here.

    You can use the key ‘meta_query‘ to filter posts by multiple meta keys like so:

    $args = array(
    'post_type' => 'Event',
        'posts_per_page' => 1000,
        'orderby' => 'meta_value_num',
        'order' => 'ASC',
        'meta_query' => array(
                    'relation' => 'OR',
            array(
                    'key' => 'event_informations_show_on_the_homepage',
                    'value' => 'yes',
            ),
            array(
                    'key' => 'event_informations_date',
                    'value' => 'yes',
            )
         )
    );
    $query = new WP_Query( $args );
    

    What WordPress is doing here is creating multiple wheres against the same column by using innerjoins on the same table, each time using a different alias. It’s pretty cool & is probably the fastest way to query like that.

    For more information see here: http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters

    Hope this helps 🙂