Skip to content

Instantly share code, notes, and snippets.

@omurphy27
Last active November 9, 2023 23:05
Show Gist options
  • Save omurphy27/da171530dc8b0db62ad6 to your computer and use it in GitHub Desktop.
Save omurphy27/da171530dc8b0db62ad6 to your computer and use it in GitHub Desktop.
Wordpress WP_Query in WooCommerce for a Product Category aka Taxonomy which is ordered by a Custom Field
<?php
$args = array(
'posts_per_page' => 7,
'post_type' => 'product',
'product_cat' => 'home-featured',
'meta_key' => 'home_featured_order',
'orderby' => 'meta_value_num',
'order' => 'DESC'
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
echo '<ul>';
while ( $query->have_posts() ) : $query->the_post();
global $product;
echo '<li>' . the_title() . ' <span>'. $product->get_price_html() .'</span></li>';
endwhile;
echo '</ul>';
wp_reset_postdata();
}
// If you want to query some other custom taxonomy and post type
// ..not a category...then your args
// would look like below
$args = array(
'post_type' => 'tribe_events',
'tax_query' => array(
array(
'taxonomy' => 'tribe_events_cat',
'field' => 'slug',
'terms' => 'featured',
),
),
);
$query = new WP_Query( $args );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment