Support

Account

Home Forums General Issues get_posts, filtering by acf date field

Solved

get_posts, filtering by acf date field

  • Hi there

    I need to use the get_posts() function together with a filter, filtering an ACF field event_date.

    I read http://www.advancedcustomfields.com/resources/query-posts-custom-fields/
    and https://rudrastyh.com/wordpress/meta_query.html

    I thought I had it when I read about meta_query where I could pass in ACF fields and I have this, and I expected it to return just posts that have an event_date bigger/equal a month earlier from today…

    
    $startDate = date('Y-m-d', strtotime('-30 days'));
    
    	$posts = get_posts([
    		'post_type' => 'events',
    		'post_status' => 'publish',
    		'meta_query', array( array( 'key' => 'event_date', 'value' => $startDate, 'compare' => '>=', 'type' => 'DATE' )),
    		'numberposts' => 50,
    	]);
    
    	print("<pre>".print_r($posts,true)."</pre>");
    

    But when I log the $posts array I always get every event!
    What am I doing wrong?

    An event_date looks like this in the REST API: event_date: "2019-10-28 18:00:00"

    I would be really glad if somebody could help me out with this.
    I also don’t get any errors…

    Thanks in advance
    J

  • Try adding this:

    'order'          => 'DESC',
    'orderby'        => 'meta_value',

    Or if you don’t plan on adding more meta_query params, this format may work too:

    $replaceme = get_posts([
        'posts_per_page' => -1,
        'post_type'      => 'custom_post_type',
        'meta_key'       => 'custom_field_name',
        'meta_compare'   => '>=', // optional
        'meta_value'     => 'custom_field_value',
    ]);

    I also don’t know the exact syntax but just play with it until it works. 🙂

  • Thank you for your input.

    Well I tried your first idea, and it did not work.
    It is just exactly the same.
    It is as if the filter is just not applied at all.
    Honestly I would have wondered if adding an order would have resolved it.

    I then tried your second suggestion, because I indeed don’t need another meta field.
    But this also does not work. Exactly the same as above.

    Playing with the syntax is difficult if I don’t see any effect at all…

    Am i missing something with the syntax? Is there something with Date fields that is different from other filter comparisons? Is there any way to gain more insight what my filter actually does? Like logging an actual comparison, to see which dates are compared and what the outcome of it is?

    Thanks in advance.
    Cheers

  • thanks my issue has been fixed ...

  • @iosman123
    Could you maybe share your code. I still had no success in achieving a proper filter. I just keep getting all posts at a time…

  • A date field is not stored as a date in the database. Remove the “type” argument from your meta query. Also alter the date format.

    
    $startDate = date('Ymd', strtotime('-30 days'));
    
    	$posts = get_posts([
    		'post_type' => 'events',
    		'post_status' => 'publish',
    		'meta_query', array( array( 'key' => 'event_date', 'value' => $startDate, 'compare' => '>=')),
    		'numberposts' => 50,
    	]);
    
    	print("<pre>".print_r($posts,true)."</pre>");
    
  • @hube2
    thanks for your input.
    I tried this as well, but unfortunately it does not have any effect either.

    When I print out the event_date I get a value like 2019-12-04 19:30:00.
    I think with your code I am comparing my $startDate which is something like 20191201 to 2019-12-04 19:30:00.

    I assume to use a compare operator of > I need to have actual numbers.
    So using the $startDate works but we would have to compare it against 20191204 instead 2019-12-04 19:30:00.

    Could that be the issue?
    And how would I transform the event_date within get_posts

    Thanks for your input.Cheers

  • the value that you see when you use get_field() on a date field is based on the settings of the field, not what is stored in the database. A date field is stored in the format “Ymd”. Are you sure you’re using a date field? Maybe you’re using a date/time field?

  • Yeah sorry, it is clearly a date/time field.

  • For a date/time field you need to use the format “Y-m-d H:i:s”

    
    $startDate = date('Y-m-d H:i:s', strtotime('-30 days'));
    
  • Unforunately the filtering still does not work:
    I have now:

    
    $startDate = date('Y-m-d H:i:s', strtotime('-2 days'));
    
    $posts = get_posts([
    		'post_type' => 'veranstaltungen',
    		'post_status' => 'publish',
    		'order' => 'DESC',
    		'orderby' => 'meta_value'.
    		'meta_query', array( array( 'key' => 'event_date', 'value' => $startDate, 'compare' => '>=')),
    		'numberposts' => -1,
    	]);
    

    But I would still get all my events displayed. Also clearly the ones that have an event_date smaller than the $startDate

    What would I have to add to get i to work?
    Is it really possible to compare

    2019-11-20 19:30:00 >= 2019-12-02 10:43:05

    I feel like that is not gonna work…

  • Anything, anyone? I really need this to get working, but I haven’t found any solution…

  • Try using WP_Query() instead of get_posts().

  • Thanks. Years after I had time to go back to this project.
    WP_Query did the trick:

    	
            date_default_timezone_set("Europe/Zurich");
    	$dateNow = date('Y-m-d H:i:s');
    
            $args = array(
    		'post_type' => 'veranstaltungen',
    		'post_status' => 'publish',
    		'order' => 'DESC',
    		'orderby' => 'meta_value',
    		'meta_query' => array(
    			array(
    				'key'           => 'event_date',
    				'compare'       => '>=',
    				'value'         => $dateNow,
    				'type'          => 'DATETIME',
    			),
    		),
    		'numberposts' => -1,
    	);
    
    	$query = new WP_Query($args);
    
    	$posts = $query->posts;
    
Viewing 14 posts - 1 through 14 (of 14 total)

The topic ‘get_posts, filtering by acf date field’ is closed to new replies.