wordpress - PHP foreach loop force certain children to be at top
Get the solution ↓↓↓I have the belowforeach
loop running over an array of WooCommerce products, which are queried by the below query. This outputs the products in themenu_order
, which is as expected, however what I need to do is to keep them in this order, EXCEPT products which as 'featured products'. These should almost be ignored by the order parameter and always appear first in theforeach
loop.
Is this possible?
// Get Products
$args = array(
'status' => 'publish',
'category' => $currentCat->slug,
'orderby' => 'menu_order',
'order' => 'ASC',
'limit' => -1,
);
$productsData = wc_get_products( $args );
<? // For each product
foreach($productsData as $product): ?>
<? if( $product['isFeatured'] ): ?>
// Output featured product html here...
<? else: ?>
// Output other products HTML here...
<? endif ?>
<? endforeach ?>
Answer
Solution:
I haven't tried this, but you could see if this works. I know this trick works with meta queries, so It might work with taxonomy queries too: the trick being to reference the tax queries by name in the orderby.
$featured_tax_query[] = array(
'taxonomy' => 'product_visibility',
'field' => 'name',
'terms' => 'featured',
'operator' => 'IN',
);
$non_featured_tax_query = $featured_tax_query;
$non_featured_tax_query['operator'] => 'NOT IN';
$args = array(
'status' => 'publish',
'category' => $currentCat->slug,
'orderby' => 'menu_order',
'order' => 'ASC',
'limit' => -1,
'tax_query' => array('featured' => $featured_tax_query,
'non_featured' => $non_featured_tax_query),
'orderby' => array(
'featured' => 'ASC',
'non_featured' => 'ASC',
)
);
$featuredProductsData = wc_get_products( $args );
If the above doesn't work, you can break it out into 2 different queries like so:
$tax_query[] = array(
'taxonomy' => 'product_visibility',
'field' => 'name',
'terms' => 'featured',
'operator' => 'IN',
);
// Get featured products
$args = array(
'status' => 'publish',
'category' => $currentCat->slug,
'orderby' => 'menu_order',
'order' => 'ASC',
'limit' => -1,
'tax_query' => $tax_query,
);
$featuredProductsData = wc_get_products( $args );
foreach($featuredProductsData as $product): ?>
// Output featured product html here...
<?php endforeach; ?>
// Get non-featured products
$tax_query['operator'] => 'NOT IN';
$args['tax_query'] => $tax_query;
$productsData = wc_get_products( $args );
foreach($productsData as $product): ?>
// Output other products HTML here...
<?php endforeach; ?>
Answer
Solution:
Managed to resolve this for my needs by performing 2 separate queries, and then merging them into an array (in the desired order). This ensures one query always appears before the other, whilst each query can still respect ordering parameters such asmenu_order
.
// Featured products query
$featuredArgs = array(
'status' => 'publish',
'category' => $currentCat->slug,
'orderby' => 'menu_order',
'order' => 'ASC',
'limit' => 1,
'tax_query' => array(
array(
'taxonomy' => 'product_visibility',
'field' => 'name',
'terms' => 'featured',
'operator' => 'IN',
),
),
);
// Non-featured products query
$nonFeaturedArgs = array(
'status' => 'publish',
'category' => $currentCat->slug,
'orderby' => 'menu_order',
'order' => 'ASC',
'limit' => -1,
'tax_query' => array(
array(
'taxonomy' => 'product_visibility',
'field' => 'name',
'terms' => 'featured',
'operator' => 'NOT IN',
),
),
);
// Get products from each query (performed seperately so that we can control the order of products)
$featuredProductsData = wc_get_products( $featuredArgs );
$nonFeaturedProductsData = wc_get_products( $nonFeaturedArgs );
// Merge both queries in the desired order
$productsData = array_merge($featuredProductsData, $nonFeaturedProductsData);
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: failed to create image decoder with message 'unimplemented'
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.