Support » Plugins » Hacks » WP_query – joining multiple custom fields

  • Hi everybody, trying to find a solution. Brief description:

    Post type “educational_course”, each has several custom fields:


    These custom fields are dates in DD.MM.YYYY format, each field can be empty of filled.

    Now I need to echo all dates and titles. U can see, several posts can be present multiple times in query according it has zero, one or several dates filled up.

    How to construct the query? Is it possible to do something similar to “select all posts with this date” AND “select all posts with another date” AND “sort all data according to DATE ascendent?”

    I know only solution for “select all posts with THIS AND THESE”, but not “THIS OR THESE”.

    Sorry for my english 🙂 and thanks for help.

Viewing 3 replies - 1 through 3 (of 3 total)
  • esmi


    Forum Moderator

    If you want to select posts using dates in custom fields, you’ll need to change the format to YYYY-MM-DD. See for a similar discussion on this.

    Thanks esmi. I know this could be complicated and I need a little more explanation.

    In my example structure could be simple like this:

    id         title         custom_field      field_value (DD.MM.YYYY)
    1          course 1      date_from_1       01.08.2011
    1          course 1      date_from_2       01.09.2011
    1          course 1      date_from_3       01.10.2011
    2          course 2      date_from_1       05.09.2011
    3          course 3      date_from_1       10.09.2011
    3          course 3      date_from_2       10.10.2011

    Each “educational_course” post type could have one or more date_from_x defined. Now I need to loop ALL dates no matter the course was already queried before by another date. One course should be multiple times in the loop if it has more than one valid date_from_x fields.

    The final output echo on page should look like this:

    01.08.2011 - course 1
    01.09.2011 - course 1
    05.09.2011 - course 2
    10.09.2011 - course 3
    01.10.2011 - course 1
    10.10.2011 - course 3

    What steps should I do to achieve that?

    1) Select all posts in one or more queries

    I think the code below can’t do it. It will select portfolio posts that have BOTH dates not empty, right?

    $args = array(
    	'post_type' => 'educational_course',
    	'meta_query' => array(
    			'key' => 'date_from_1',
    			'value' => '',
                            'type' => 'date',
    			'compare' => '!='
    			'key' => 'date_from_2',
    			'value' => '',
                            'type' => 'date',
    			'compare' => '!='
                    ... // other date_from_X
    $query = new WP_Query( $args );

    So I think it will be needed to join multiple queries via filters of wp_query like “posts_join” or “posts_orderby”. Unfortunately there is a lack of documentation how to do that, see

    2) Reorder the data acording to date_from_x field.
    A hard task. Really don’t know how to do that since there are more keys.

    No one able to help? Come on! 🙂

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘WP_query – joining multiple custom fields’ is closed to new replies.