WordPress.org

Forums

WP_query - joining multiple custom fields (4 posts)

  1. p.beloch
    Member
    Posted 4 years ago #

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

    Post type "educational_course", each has several custom fields:

    "date_from_1"
    "date_from_2"
    "date_from_3"...

    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.

  2. esmi
    Forum Moderator
    Posted 4 years ago #

    If you want to select posts using dates in custom fields, you'll need to change the format to YYYY-MM-DD. See http://wordpress.org/support/topic/need-help-caluclating-future-date-in-the-loop-using-custom-field?replies=6 for a similar discussion on this.

  3. p.beloch
    Member
    Posted 4 years ago #

    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:

    Calendar
    --------
    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(
    		array(
    			'key' => 'date_from_1',
    			'value' => '',
                            'type' => 'date',
    			'compare' => '!='
    		),
    		array(
    			'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 http://codex.wordpress.org/Function_Reference/WP_Query.

    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.

  4. p.beloch
    Member
    Posted 3 years ago #

    No one able to help? Come on! :-)

Topic Closed

This topic has been closed to new replies.

About this Topic