I have a taxonomy called coupon_category
. In a query_post
I am trying to call all posts from related custom taxonomy with the same coupon_category
.
If I use:
<?php
// show all active coupons for this category from related store and setup pagination
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts( array(
'post_type' => APP_POST_TYPE,
'post_status' => 'publish',
'posts_per_page' => 4,
'tax_query' => array(
array(
'taxonomy' => 'coupon_category',
'field' => 'slug',
'terms' => 'mode',
),
)
) );
?>
I can show all related posts with the term "mode" however I would like to automate it, so that always the terms (coupon_category
) that are already in use on the page are shown.
I have a taxonomy called coupon_category
. In a query_post
I am trying to call all posts from related custom taxonomy with the same coupon_category
.
If I use:
<?php
// show all active coupons for this category from related store and setup pagination
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts( array(
'post_type' => APP_POST_TYPE,
'post_status' => 'publish',
'posts_per_page' => 4,
'tax_query' => array(
array(
'taxonomy' => 'coupon_category',
'field' => 'slug',
'terms' => 'mode',
),
)
) );
?>
I can show all related posts with the term "mode" however I would like to automate it, so that always the terms (coupon_category
) that are already in use on the page are shown.
You'll have to get the terms of current post with get_the_terms
function and then use them in your query.
// this will get terms and then get only term_ids from them
$term_ids = wp_list_pluck( get_the_terms( get_the_ID(), 'coupon_category' ), 'term_id' );
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$related = new WP_Query( array(
'post_type' => APP_POST_TYPE,
'post_status' => 'publish',
'posts_per_page' => 4,
'tax_query' => array(
array(
'taxonomy' => 'coupon_category',
'field' => 'term_id',
'terms' => $term_ids,
),
)
) );
So we use get_the_terms
to get list of terms for current post and then we call wp_list_pluck
, which is a very handy function, that will get array containing only one field from all of given objects.
PS. I've also changed your query_posts
to WP_Query
- it's better for your efficiency.
Okay after a lot of reading I finally changed to 'wp_query' and this is the perfect solution:
$tax = get_the_terms($id, 'coupon_category');
$args = array(
'post_type' => APP_POST_TYPE,
'post_status' => 'publish',
'posts_per_page' => 5,
'meta_key' => 'clpr_topcoupon',
APP_TAX_TAG => 'gutschein',
'orderby' => array(
'meta_value_num' => 'DESC',
'post_date' => 'DESC',
),
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'coupon_category',
'field' => 'slug',
'terms' => $tax[0]->slug,
),
array(
'taxonomy' => APP_TAX_STORE,
'field' => 'slug',
'terms' => $term->slug,
'operator' => 'NOT IN',
),
),
);
$query = new WP_Query( $args );
while ( $query->have_posts() ) :
$query->the_post();
get_template_part( 'loop', 'coupon' );
endwhile;
wp_reset_postdata();
Found a solution, quite similar to yours:
$tax = get_the_terms($id, 'coupon_category');
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts( array(
'post_type' => APP_POST_TYPE,
'post_status' => 'publish',
'posts_per_page' => 4,
'tax_query' => array(
array(
'taxonomy' => 'coupon_category', // Taxonomy
'field' => 'slug',
'terms' => $tax[0]->slug, // Your category slug
),
)
) );
Now I only need to exclude post from APP_TAX_STORE
with current slug or from taxonomy stores
.