Support

Account

Home Forums General Issues Querying Multiple Post Types

Solved

Querying Multiple Post Types

  • Hi I’m trying to construct a query to display a single post that matches the following criteria:

    post_type = ‘friday’ OR ‘saturday’ OR ‘sunday’
    AND
    custom_text_field = ‘right’
    AND
    custom_date_field_1 < current_date < custom_date_field_2

    I was able to get the OR relationship working for custom fields but I can’t seem to get it to work for post types. Also, if there is a way to just avoid specifying post types and just querying all posts of any type, that might work out better for me. Any ideas? Thanks.

  • Hi @kevinreilly

    All the WP_Query args are well documented on the WP website. I will have to refer you for your questions regarding post_type as they are not related to ACF.

    Perhaps you can post back your actual $args for the WP_Query object and I can help you fix any issues regarding the meta_query args?

    Thanks
    E

  • Here is my current code. I’ve resolved the issue of querying multiple post types but now I need to only get posts that have:
    date_picker_value1 <= current_date <= date_picker_value2

    $args = array (
        'post_type' => array('friday','saturday','sunday','special'),
        'meta_query' => array(
    	array(
    	    'key' => 'publish_area',
    	    'value'     => 'left',
    	),
        ),
    );
  • Hi @kevinreilly

    As long as your date field is saved with the default format (JS format = yymmdd), then you can compare the values like so:

    
    <?php 
    
    $today = date('Ymd');
    
    $args = array (
        'post_type' => array('friday','saturday','sunday','special'),
        'meta_query' => array(
    		array(
    		    'key' 	=> 'publish_area',
    		    'value'	=> 'left',
    		),
    		array(
    	        'key'		=> 'start_date',
    	        'compare'	=> '<=',
    	        'value'		=> $today,
    	    ),
    	     array(
    	        'key'		=> 'end_date',
    	        'compare'	=> '>=',
    	        'value'		=> $today,
    	    )
        ),
    );
    
    ?>
    

    Please note this code is untested, but should explain how to do it!

    Thanks
    E

Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Querying Multiple Post Types’ is closed to new replies.