Support

Account

Home Forums General Issues How to query on custom field name not on publishing date

Solving

How to query on custom field name not on publishing date

  • Hello
    I am stuck with the issue:

    I have a loop section in my websites, where I want to show events. Elementor/Wordpress shows all post related on the publishing date. But I want to show the posts with the event-date (‘datum’). And only from today onwards. How can I manage that?

    I tried also with the code:

    function my_pre_get_posts( $query ) {

    // do not modify queries in the admin
    if ( is_admin() ) {
    return $query;
    }

    // only modify queries for the main loop
    if ( $query->is_main_query() ) {

    // only modify queries on the events archive page
    if ( is_post_type_archive( ‘events’ ) ) {

    // Set meta query to filter by ‘datum’ field
    $today = date( ‘Ymd’ ); // Format: YYYYMMDD
    $meta_query = array(
    array(
    ‘key’ => ‘datum’, // Replace with your ACF field name
    ‘value’ => $today,
    ‘compare’ => ‘>=’,
    ‘type’ => ‘DATE’,
    ),
    );

    $query->set( ‘post_type’, ‘events’ );
    $query->set( ‘orderby’, ‘meta_value’ );
    $query->set( ‘meta_key’, ‘datum’ ); // Replace with your ACF field name
    $query->set( ‘order’, ‘ASC’ ); // Order: oldest date first
    $query->set( ‘meta_query’, $meta_query );
    }
    }

    // return
    return $query;
    }
    add_action( ‘pre_get_posts’, ‘my_pre_get_posts’ );

  • ACF date fields are not ‘type’ => ‘DATE’,

    You can use string or number comparison

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

You must be logged in to reply to this topic.