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

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.

Related

Wordpress sort by number custom fields gives different results

I have a query which gives me custom post type posts which are sorted by category and a custom fields which has a number that indicates the amount of votes for the post. The problem is, if 2 posts have 3 votes, sometimes when you refresh, it changes their position. Here is the code:
$args = array(
'numberposts' => 10,
'post_type' => 'nominees',
'meta_query' => array(
array(
'key' => 'category',
'value' => get_the_ID(),
'compare' => '=',
)
),
'orderby' => 'meta_value_num',
'order' => 'DESC',
'meta_key' => 'count_nominees'
);
I tried adding order on the category meta query, but nothing changes, I still sometimes get different results when refreshing, if the posts have same amount of votes. What is the right way to add second order, by ID or something?
Thanks.
As mentioned in the comments, I think this might help, but you might be able to extend it to be a little more specific in the search query for your use case.
$args = array(
'numberposts' => 10,
'post_type' => 'nominees',
'meta_query' => array(
array(
'key' => 'category',
'value' => get_the_ID(),
'compare' => '='
)
),
'meta_key' => 'count_nominees',
'orderby' => array(
'count_nominees' => 'DESC',
'ID' => 'DESC'
)
);
That should get 10 posts in the nominees post type, only if they're part of category xyz, and have post meta of count_nominees and order by count_nominees first in descending order and then by the post ID in descending order.
You can use the WP_Query documentation on the WordPress codex for more information about complex queries using post meta data.

reuse wp query with different meta query

I am querying a custom post type. I would like to output posts which fall after a custom date field which I can do fine. But then I want to reuse the same basic query to output posts which fall before the custom date field.
So I have
$today = date('Ymd');
$args = array(
'post_type' => array( 'mypoststype' ),
'posts_per_page' => '3',
'meta_query' => array(
array(
'key' => 'mypoststype_date',
'compare' => '<=',
'value' => $today,
)
),
'orderby' => 'meta_value_num',
'order' => 'ASC',
);
$myposts= new WP_Query( $args );
then I output the necessary contents in a loop. All fine.
Next I want to have posts from the same type ordered by the same key but with compare set to '>='.
Is there some way to do this without calling a whole new query?

Wordpress AJAX Load More Orderby Meta_VALUE_NUM

Helo,
I have multiple meta_key value pairs inside my custom posts. So to work around this what I did is that I ran a WP_QUERY initially which creates an array of posts ID which I then pass to load more to load those.
The array that I build using WP_Query is in the correct order. However when I use the IDs in post_in parameter it is not displaying them in the given order. I have also tried to give orderby="meta_value_num" parameter but it doesnt work either.
Here is WP_QUERY which is working perfectly
$loop = new WP_Query(
array(
'post_type' => 'properties',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'listing_type',
'value' => array(3,2),
'type' => 'NUMERIC',
),
array(
'key' => 'payment_status',
'value' => 'yes',
),
array(
'key' => 'expired',
'value' => 'no',
- ),
),
'orderby' => 'meta_value_num',
'meta_key' => 'listing_type',
'order' => 'DESC'
));
Here is the shortcode:
[ajax_load_more post_type="properties" post__in="'.implode(',',$featured).'" posts_per_page="10" scroll="false" transition="fade" button_label="'.$l_more.'" button_loading_label="'.$l_more_2.'" container_type="ul" css_classes="items",orderby="meta_value_num" meta_key="listing_type"]
However it doesnt order posts as it should because $featured array has it in required order. Even if I remove order by and meta_key parameter it doesnt work.
Please Help
Ahmar
First, you have an error in your shortcode.
[ajax_load_more post_type="properties" post__in="'.implode(',',$featured).'" posts_per_page="10" scroll="false" transition="fade" button_label="'.$l_more.'" button_loading_label="'.$l_more_2.'" container_type="ul" css_classes="items" orderby="meta_value_num" meta_key="listing_type"]
Second, you should orderby="post__in" to preserve the post__in ordering.
https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters

How do you order by 2 meta values in Wordpress?

I am using Woocommerce, although my question relates to WP_Query in general. I am creating a custom WP_Query loop for bestselling products and I want to allow users to be able to sort the results by price, from high to low and low to high. The problem is in Woocommerce both _price and total_sales are meta fields and as far as I can tell I can only orderby 1 meta field in Wordpress loops. Is there a way around this?
My full code including some of my attempts is here, the most relevant code segment looks like as follows:
$queryArgs = array(
'post_type' => 'product',
'posts_per_page' => -1,
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => '_visibility',
'value' => array('catalog', 'visible'),
'compare' => 'IN'
)
),
'meta_key' => '_price',
'orderby' => array('meta_value_num' => $sortBy['terms'])
);
The actual code is more complicated than this because it's built for a range of filtering and sorting options, but this is the gist of it and bestsellers is the only thing giving me issues because of the two meta_keys. I've read this but it doesn't apply to custom meta fields.
I've tried:
'meta_key' => '_price total_sales'
'meta_key => array('_price', 'total_sales')
'orderby' => array('meta_value_num' => $sortBy['terms'], 'meta_value_num' => 'DESC')
Nothing seems to work. I've also tried hooking onto the various WP_Query filters but the problem is this query is part of a dynamically generated loop so I can't 'hack' or hard-code anything.
Use the meta query with arrays:
'meta_query' => array (
array (
'key' => '_visibility',
'value' => array('catalog', 'visible'),
'compare' => 'IN'
),
array (
'key' => '_price',
'value' => "VALUE WHAT YOU WANT, NOT ASC/DESC",
)
),

WP_Query with Custom Field Parameter (checkbox)

I'm trying to set up a custom query that returns only posts that do not have a custom meta field checkbox is checked. The checkbox meta field id is position-filled.
Here is the code I have:
$args = array (
'post_type' => 'jobs',
'posts_per_page' => 4,
'post__not_in' => array(get_the_id()),
'meta_query' => array(
array(
'key' => 'position-filled',
'value' => 'on', // also tried passing null and removing 'compare'
'compare' => '!=' // also tried 'NOT LIKE'
)
),
);
$customQuery = new WP_Query($args);
However, this returns no posts (I have made certain there are posts without the checkbox checked.
Any idea what I'm doing wrong?
I've run into similar issues when adding custom fields to existing posts/pages where I needed to search on "unchecked" or "unselected" fields.
It boils down to the fact that the query is actually saying, "Give me posts where the meta key (position-filled) is populated, but not populated with a value of 'on'".
Does it work if you re-save your post with the checkbox being unchecked?
If so, you might also try using the 'compare' value of 'NOT EXISTS'. Although I believe if you use NOT EXISTS and then someone saves an older post without the checkbox being checked, it will exist (with a blank value).
[edit]
I forgot to mention that you can build a query with either situation being true (the NOT EXISTS or it does exist but the value is blank/unchecked).
$args = array(
'post_type' => 'jobs',
'posts_per_page' => 4,
'post__not_in' => array(get_the_id()),
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'position-filled',
'value' => 'on',
'compare' => '!='
),
array(
'key' => 'position-filled',
'value' => '',
'compare' => 'NOT EXISTS'
)
)
);
You can chain multiple comparisons together and using the relation property of OR, either one should return true.

Categories