Support

Account

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

Solving

Using WP meta query to show custom post types by a start and finish date

  • I have a custom post type called ‘events’, I have a page which displays events by each month. Each event has a start_date and finish_date custom field (using advanced custom fields plugin) which stores the date as yymmdd. If an event spans over two different months it is only appearing in the month of the start date. I’m stuck on how to show it in the month of the finish date as well. For example, if I have an event with a start date of 19/03/2014 and a finish date of 19/04/2013 it should appear in both the month of March and April.

    I am using the below:

    	// Get chosen month to display from URL
    	$events_month = sanitize_text_field($_GET["month"]);
    	$events_year = sanitize_text_field($_GET["year"]);
    	
    	// Convert chosen month to display to a timestamp
    	$ts = strtotime("$events_month $events_year");
    	
    	// Create chosen month start end end dates to use for query
    	$month_start_date = date('Ym01', $ts);
    	$month_end_date = date('Ymt', $ts);
    	
    	$args = array(
    		'post_type' => 'events',
    		'posts_per_page' => 50,
    		'order' => 'ASC',
    		'orderby' => 'meta_value_num',
    		'meta_key' => 'start_date',
    		'meta_query'		=> array(
    			array(
    				'key' => 'start_date',
    				'value' => array($month_start_date, $month_end_date),
    				'type' => 'numeric',
    				'compare' => 'BETWEEN'
    			)
    		)
    	);
    	
    	$events = new WP_Query($args);
  • 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);
    
  • Hi thanks for the suggestion but I don’t think you’ve understood what I’m trying to do and your code cannot work for this. Going back to my example:

    If an event spans over two different months it is only appearing in the month of the start date. For example, if I have an event with a start date of 19/03/2014 and a finish date of 19/04/2013 it should appear in both the month of March and April, but not for now it can only appear in March, because in April it will fail when checking whether the start date is greater that the month start date (it started the month before)

    Any other ideas? Thanks again

  • Are you sure your logic is working? Also where does the get parameters come from (obviously the URL but how are they set there)?

    Your date functions, shouldn’t they be set like the value saved in the database

    
    date(Ymd); /Gives YYYMMDD
    

    Also try setting the type value to DATE in the query.. (read the text here:http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters)

  • Maybe my logic is not correct but what I’m trying to achieve should be simple: none of our above code can work because as our code currently is, it will only show the events in a chosen month that has both the start date and finish date in that month. I need the events to show in all possible months they span over.

  • The GET parameters just come from the URL for example: ?month=March&year=2014

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

The topic ‘Using WP meta query to show custom post types by a start and finish date’ is closed to new replies.