WordPress.org

Ready to get started?Download WordPress

Forums

WP Query date_query problem (3 posts)

  1. SDM36
    Member
    Posted 2 months ago #

    Hiya,

    Basically I'm trying to order a bunch of posts by season; 'spring', 'summer' and what have you.

    It occurred to me that the easiest way of achieving this was to run four new WP_Query's using date query for each season.

    To that end I got this far:

    <?php $args = array(
    		'posts_per_page' => -1,
    		'cat'	=> 19,
    		'date_query' => array(
    					array(
    						'month' => 1,
    						'month' => 2,
    						'month' => 3
    						 ),
    				'relation' => 'OR'
    				 )
    			); 	?>
    <?php $spring = new WP_Query($args); ?></p>
    <p><?php while ( $spring->have_posts() ) : $spring->the_post(); ?>
    <?php print_r($post); ?>
    <?php endwhile; ?>
    <?php wp_reset_postdata(); ?>

    Yet that doesn't return anything.
    It's definitely to do with the date_query array as when I remove it all posts come out fine.

    I've followed the advice and format as per the codex and am left scratching the old head.

    Anyone able to give me a quick pointer please?

  2. bcworkz
    Member
    Posted 2 months ago #

    I'm not that familiar with date_query, as it's relatively new, as a guess I believe you cannot have multiple 'month' arguments in a single array. Have you tried each one in it's own array, all the arrays related by OR?

    Or why not just have an inclusive date range? The Codex example is for Jan 1 - Feb 28, obviously you could extend it to Mar 31, or for that matter do Dec 23 to Mar 20 (or whatever is the actual winter dates). By using the array format, you can omit the year arguments for all winter dates regardless of year.

  3. Steven Jones
    Member
    Posted 2 months ago #

    There might be a better way of doing this, however you'll probably need to break the months into an array per month.

    'date_query' => array(
    		array(
    			'month'      => 1,
    			'compare'   => '=',
    		),
    		array(
    			'hour'      => 2,
    			'compare'   => '=',
    		),
                   'relation' => 'OR'
    	),

    This is untested though, but should workl

Reply

You must log in to post.

About this Topic