I would like to show 4-5 posts by thumbnail and title below each product. For all products I have added a custom taxonomy to group all these products under their own group term. A custom tag so to speak.
Now I want to query 5 posts under each product that share that same term chosen for custom taxonomy group. So far I have created this to query the posts by taxonomy and term:
function shortcode_imwz_custom_taxonomy_by_term() {
global $wp_query,$post;
// wp_get_object_terms( $post->ID, 'portfolio-skills', array( 'fields' => 'names' ) );
// https://stackoverflow.com/a/14798097/460885
// $product_terms = wp_get_object_terms( $post->ID, 'product' );
// For performance, functions like get_the_terms() (which the results of has been cached),
// should be used.
// get the terms Retrieve the terms of the taxonomy that are attached to the post.
// $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
// https://wordpress.stackexchange.com/a/21425/12260
$terms = get_the_terms( get_the_ID(), 'group' );
$loop = new WP_Query( array(
'posts_per_page' => 5,
'post_type' => 'product',
'orderby' => 'menu_order title',
'order' => 'ASC',
'tax_query' => array( array(
'taxonomy' => 'group',
'field' => 'slug',
'terms' => $terms
) )
) );
if( ! $loop->have_posts() ) {
return false;
}
while( $loop->have_posts() ) {
$loop->the_post();
// echo thumbnail
echo the_title();
}
wp_reset_postdata();
}
I added a shortcode based on this code
function register_grouped_products_shortcode() {
add_shortcode( 'grouped-products', 'shortcode_imwz_custom_taxonomy_by_term' );
}
add_action( 'init', 'register_grouped_products_shortcode' );
to a product to use the WP Query and did add a term of choice. But I do not see anything loaded yet. What am I missing here? So many options I found to get the terms to filter posts on taxonomy AND term entered.. Perhaps an issue here?