Support

Account

Home Forums General Issues Using WP meta query to show custom post types by a start and finish date Reply To: Using WP meta query to show custom post types by a start and finish date

  • You could possible ditch the BETWEEN and instead use two meta queries, I’ve done this myself a while back. Here’s some code modified to your example

    
    //gets all posts that has not yet ended
    $args = array(
    	'post_type' => 'events',
    	'orderby' => 'date',
    	'posts_per_page' => 50,
    	'order' => 'ASC',
    	'orderby' => 'meta_value_num',
    	'meta_key' => 'start_date',
    	'meta_query'=> array(
    		array(
    			'key' => 'end_date',
    			'compare' => '>',
    			'value' => "$month_end_date",
    		)
    		array(
    			'key' => 'start_date',
    			'compare' => '<',
    			'value' => "$month_start_date",
    		)
    	)		        
    );
    			
    $events = new WP_Query($args);