最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Why do WP_Query results change after updating unrelated Advanced Custom Fields (ACF)?

programmeradmin0浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论