0

I have a frustrating issue. I am using the date_query as referenced in the Codex:

<?php
$today = getdate();
$args = array(
'post_type'         => 'Lighting',
'post_status'       => 'publish',
'posts_per_page'    => 1,
'date_query' => array(
        array(
            'year'  => $today['year'],
            'month' => $today['mon'],
            'day'   => $today['mday'],
        ),
    ),
);
$the_query = new WP_Query( $args );

While I can display custom posts as expected if I exclude 'day', I get no posts when it is included in the query. I am using Advanced Custom Fields Pro with the date-picker. I have no idea why this is happening, and I've searched tirelessly to determine why my date_query is not working. I am able to echo the date, so I don't understand where the disconnect is.

/****** RESPONSE TO ANSWERS ******/

Thanks for the responses. I have done a meta_query with what I think is the proper date format, but I am still unable to query only today's post. Here is the new query:

<?php // Let's get the data we need to loop through below

$args = array( 
        'post_type' => 'carillon', // Tell WordPress which post type we want
        'orderby' => 'meta_value', // We want to organize the events by date    
        'meta_key' => 'date_of_lighting', // Grab the "date_of_event" field created via "date-picker" plugin (stored in ‘YYYYMMDD’)
        'posts_per_page' => '1', // Let's show just one post.   
        'meta_query' => array( // WordPress has all the results, now, return only the event on today's date
            array(
                'key' => 'date_of_lighting', // Check the s"date_of_lighting field
                'value' => date("Y-M-D"), // Set today's date (note the similar format)
                'compare' => '=', // Return only today's post
                'type' => 'NUMERIC' // Let WordPress know we're working with numbers
                )
            )
    );

Any suggestions? Thanks, again.

/******** SOLUTION **********/

Hi Everyone,

So, I found a solution that works. I changed the type to "DATE". It's interesting that in other examples where folks want to show todays date and beyond, they use a "Numeric" type. I guess it makes some sense, but I'm going to jump into the Codex to understand this more. I appreciate all of your help. Here is the solution that works:

                        <?php // Let's get the data we need to loop through below
                            $args = array( 
                                    'post_type' => 'carillon', // Tell WordPress which post type we want
                                    'orderby' => 'meta_value', // We want to organize the events by date    
                                    'meta_key' => 'date_of_lighting', // Grab the "date_of_event" field created via "date-picker" plugin (stored in ‘YYYYMMDD’)
                                    'posts_per_page' => '1', // Let's show just one post.   
                                    'meta_query' => array( // WordPress has all the results, now, return only the event for today's date
                                        array(
                                            'key' => 'date_of_lighting', // Check the s"date_of_lighting field
                                            'value' => date("Y-m-d"), // Set today's date (note the similar format)
                                            'compare' => '=', // Return only today's post
                                            'type' => 'DATE' // Let WordPress know we're working with a date
                                            )
                                        )
                                );
  • getdate() with no timestamp parameter given returns the array for the current local time. It looks like you don't have any posts for the day you specify in the 'date_query'. – Max Yudin Jul 4 '16 at 7:20
  • You need a meta_query. Look at the codex at the custom field parameters in WP_Query. Just a note, your date MUST BE saved as Y-m-d format or unix timestamp, any other format will NOT work, and then the date you pass as meta value MUST MATCH the fomat in which the field is saved otherwise this will also not work. There is really tons of info on-site as well, so be sure to use the site search as well – Pieter Goosen Jul 4 '16 at 9:48
  • Thanks guys. I have updated the code, but it still doesn't work. Please see the original but now amended post. Thanks in advance! – C.MO Jul 4 '16 at 21:55
0

As said in comments, you should use meta_query.

For help on how to query posts by ACF meta field, you can check the ACF documentation:

https://www.advancedcustomfields.com/resources/query-posts-custom-fields/

$args = array(
    'numberposts'   => -1,
    'post_type'     => 'event',
    'meta_key'      => 'location',
    'meta_value'    => 'Melbourne'
);


// query
$the_query = new WP_Query( $args );
0

Here is the solution folks. I've commented on each line to clarify what's going on. Hopefully, it all makes sense and is correct :)

I appreciate the help I received from this rockstar community.

        <?php // Let's get the data we need to loop through below
    $args = array( 
            'post_type' => 'carillon', // Tell WordPress which post type we want
            'orderby' => 'meta_value', // We want to organize the events by date    
            'meta_key' => 'date_of_lighting', // Grab the "date_of_event" field created via "date-picker" plugin (stored in ‘YYYYMMDD’)
            'posts_per_page' => '1', // Let's show just one post.   
            'meta_query' => array( // WordPress has all the results, now, return only the event for today's date
                array(
                    'key' => 'date_of_lighting', // Check the "date_of_lighting" field
                    'value' => date("Y-m-d"), // Set today's date (note the similar format)
                    'compare' => '=', // Return only today's post
                    'type' => 'DATE' // Let WordPress know we're working with numbers
                      )
                    )
                  );

                        $the_query = new WP_Query($args); ?>

                            <?php if ( $the_query->have_posts() ) : ?>                                  

                                    <!-- the loop-->

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.