Home › Forums › General Issues › Trying to ORDER foreach loop by ACF field
I’m trying to display the child categories of terem in a certain order. To achieve this, I added an ACF number field to the categories.
Here’s where my code is:
// Setup arguments.
$cats = get_categories(‘child_of=’.get_query_var(‘termek’));
$i = 0;
foreach ($cats as $cat) :
$number = get_field(‘number’, $cat);
$args = array(
‘post_type’ => ‘termek’,
‘posts_per_page’ => -1, // max number of post per category
‘category__in’ => array($cat->term_id),
‘meta_key’ => $number,
‘orderby’ => ‘meta_value’,
‘order’ => ‘DESC’
$my_query = new WP_Query($args);
if ($my_query->have_posts()) :
Now the issue is, I have 5 of these in total, but I started adding some numbers to them 1 – 2 – 3. Each time I add a number to the ACF field, they dissappear from the output of my loop. So not only does it not sort them, it excludes them.
What am I missing? Thanks!
Not sure I understand the question.
You say that you want to order categories but you are adding the orderby statement to a post query.
1) There isn’t any way using get_categories()/get_terms() to order terms by a CPT.
2) There isn’t any way using WP_Query() to order posts based on a field that in not saved in as post meta for posts. In this case these values would be saved in term meta for the term.
If your field is on the term then trying to short posts by a non existent meta key will result in no results found. When you specify the meta key the way you have it implies that the meta key must exist.
What would you suggest then? To clarify.
I have a CPT “Termek”
This CPT has a Category (also called / sorry for the confusion) of “Termek” which has 5 children
– Termek 1
– Termek 2
– Termek 3
– Termek 4
– Termek 5
I would like to order these based on the $number field so that if say “Termek 5” has an ACF field of $number with the value of 3, it will show up third in the foreach statement. Am I going about this in the wrong way?
I managed to fix this issue by installing the “Taxonomy Order” plugin, so the issue is taken care of.
Thanks for your help and explanation.
I have some issue regarding sorting using ACF field
In Woocommerce product I have added one ACF group named
product_shipping_and_installment and inside this group one field named
product_type in this field user will add “AC” Or “CO”.
Below is the code:
$args = array( 'post_type' => 'product', 'posts_per_page' => -1, 'post_status' => "publish", 'product_cat' => $cat->name, 'meta_query' => array( 'relation' => 'AND', array( 'key' => 'product_shipping_and_installment_product_type', 'value' => array('AC', 'CO'), 'compare' => 'IN' ), ), ); $loop = new WP_Query($args);
Above screenshot code will provide me all the list of product based on field named
product_type but not sorting wise. I would like to show “AC” value product list first and then need to show “CO” product list currently is showing me “CO” product list first and then “AC” product list.
Any help is greatly appreciate.
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
Plugin boilerplates can do some of the heavy lifting during initial development. We look at four options to speed up your plugin creation. https://t.co/ZtMsdBxAHw— Advanced Custom Fields (@wp_acf) June 5, 2023
© 2023 Advanced Custom Fields.