Support

Account

Home Forums ACF PRO WP_Query results change after updating unrelated Custom Fields (

Unread

WP_Query results change after updating unrelated Custom Fields (

  • **TLDR** – *After manually updating a custom post, my WP_Query will change it’s sorting order and move that updated post to the end of the array response.*

    I have a custom post type called Events that use Advanced Custom Fields. I have written a shortcode that uses WP_Query to find all the Events that are Open and then sort them by their final_deadline first and their priority second.

    Here are the arguments for my query:
    `
    $today_is = date(‘Y-m-d’, time());

    $open_events_args = array(
    ‘post_type’=> ‘events’,
    ‘offset’ => 0,
    ‘meta_query’ => array(
    ‘relation’ => ‘AND’,
    ‘final_deadline’ => array(
    ‘key’ => ‘final_deadline_date’,
    ‘value’ => $today_is,
    ‘compare’ => ‘>=’,
    ),
    ‘open_value’ => array(
    ‘key’ => ‘is_open’,
    ‘value’ => ‘Open’,
    ‘compare’ => ‘=’,
    ),
    ‘priority’ => array(
    ‘key’ => ‘event_priority’,
    ‘compare’ => ‘EXISTS’,
    )
    ),
    ‘orderby’ => array (
    ‘final_deadline’ => ‘ASC’,
    ‘priority’ => ‘ASC’,
    ),
    ‘post_status’ => ‘publish’,
    ‘posts_per_page’ => -1,
    ‘fields’ => ‘ids’,
    );

    $the_open_query = new WP_Query($open_events_args);
    $open_events = $the_open_query->get_posts();
    `

    Many of my advanced custom fields are being set programmatically through an API call. What I found was the WP_Query initially returned correct results, but as soon as I manually updated an event post (without changing any of the fields), that post would move to the end of the $open_events array and be out of order.

    If I updated another post, it too would move to the end of the array. Eventually the open_events array was bifurcated. The first section had events that were not manually updated, and the second section had events that were updated … and each section was in the correct order.

    I went ahead and manually updated my 17 events and figured that would fix it. However, now that I’m adding images to a new advanced custom field, this process is starting all over again. Updated events are moving to the end of the array and out of order.

    Does anyone know what would cause this and how I can fix it? Updating an unrelated custom field should not be affecting my WP_Query, but it keeps happening and I’m not sure why.

    Thanks.

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.