Hi I have a custom post type called 'support-team-doc' and a custom taxonomy called 'support_team_docs' which have categories as follows:
Support Teams Group
-- Accounts
-- IT
-- Marketing
-- Risk & Compliance
Each category has numerous posts; I do not want posts from the sub-categories to display on any of the categories pages; atm any posts in the sub categories 'accounts, it, marketing, risk-compliance' are being displayed like this: 'support-teams-group'; I have tried the following:
<?php
$termsTextarea = get_queried_object();
$args = array(
'post_type' => 'support-team-doc',
'tax_query'=>
array(
'taxonomy' => 'support_team_docs',
'field' => 'slug',
'terms' => $termsTextarea->slug,
'include_children' => false,
),
);
$query1 = new WP_Query( $args );
while ( $query1->have_posts() ) : $query1->the_post();
get_template_part( 'content', 'support_team_docs' );
endwhile; ?>
I do not know what I am doing wrong.
tax_query is a multi-dimensional array... you're missing one array element in your current code. Try this:
<?php
$termsTextarea = get_queried_object();
$args = array(
'post_type' => 'support-team-doc',
'tax_query'=>
array(
array(
'taxonomy' => 'support_team_docs',
'field' => 'slug',
'terms' => $termsTextarea->slug,
'include_children' => false
)
)
);
$query1 = new WP_Query( $args );
while ( $query1->have_posts() ) : $query1->the_post();
get_template_part( 'content', 'support_team_docs' );
endwhile; ?>
Related
I'm using the WooCommerce Subscriptions plug-in for several products in the catalog that are available as both individual or subscription products and am writing a custom template page to query products that are available as subscriptions. My query is below, everything looks right to my eyes, but for some reason it's not working. Can anyone see what's wrong here?
*Note, if I remove the 'tax_query', all the coffee products are returned as expected, but when I try to restrict by the tax_query no products are returned (and yes, I have subscription products in the coffee category).
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'product_cat' => 'coffee',
'tax_query' => array( // builds the taxonomy query
array(
'taxonomy' => 'product_type',
'field' => 'name',
'terms' => 'subscription',
),
),
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
You need to make a "tax_query" for the product category too, to avoid this problem as the way you are doing it, is deprecated since WordPress version 3.1 in favor of "tax_query". So in your code:
$loop = new WP_Query( array(
'post_type' => 'product',
'posts_per_page' => -1,
'post_status' => 'publish',
'tax_query' => array( // builds the taxonomy query
'relation' => 'AND',
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'coffee',
),
array(
'taxonomy' => 'product_type',
'field' => 'name',
'terms' => 'subscription',
)
)
) );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
It should work.
I have this query based on WordPress docs, but no result. If I remove the tax_query it gives all publish posts, but when I add it, it displays no result.
I have checked the database and I'm sure there is post attached to the wp_term_taxonomy so I'm not sure why it doesn't display results.
What I am doing here, I get all custom taxonomy type then loop through it to get all posts related to it.
$event_terms = get_terms(
'event_type',
array(
'orderby'=>'name',
'hide_empty'=>true
)
);
if(!empty($event_terms) && !is_wp_error($event_terms)){
foreach($event_terms as $event_term){
// code for each event term
$args = array(
'status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'event_type',
'field' => 'slug',
'terms' => $event_term->slug,
),
),
);
$loop = new WP_Query($args);
if($loop->have_posts()) {
echo '<h2>'.$event_term->name.'</h2>';
while($loop->have_posts()) : $loop->the_post();
echo ''.get_the_title().'<br>';
endwhile;
}
// this displays all taxonomy terms
echo $event_term->name."-".$event_term->term_taxonomy_id."<br>";
}
}
Please change your text query like this and try:
$the_query = new WP_Query( array(
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'event_type',
'field' => 'slug',
'terms' => $event_term->slug
)
)
) );
$count = $the_query->found_posts;
I hope this is helps you.
In my website, I have two different post-types. One of them is publication with custom category-type as publication-category and the other is service with custom category-type as service-category. I am publishing some brochures in Publication page those are under different services. What I am trying to do is displaying these brochures in Services page by same category type. It is like, if the brochure is published by Education services, then this brochure should be displayed in Education service page.
I am currently using ACF plugin to do so, but whenever there is new brochure, I have to go to service page and add it there. Today I tried below code but it displays all brochures from different category-types not the same category-type.
Perhaps you guys can help me how I can arrange the code a way that works for my above request.
<?php
$custom_taxterms = wp_get_object_terms(
$post->ID,
'publication-category',
array( 'fields' => 'ids' )
);
$args = array(
'post_type' => 'publication',
'post_status' => 'publish',
'posts_per_page' => 10,
'orderby' => 'rand',
'order' => 'ASC',
'tax_query' => array( array(
'taxonomy' => 'publication-category',
'field' => 'id',
'terms' => $custom_taxterms
)),
'post__not_in' => array( $post->ID ),
);
$related_items = new WP_Query( $args );
if ( $related_items->have_posts() ) :
echo '<ul>';
while ( $related_items->have_posts() ) : $related_items->the_post();
?>
<li><?php the_title(); ?></li>
<?php
endwhile;
echo '</ul>';
endif;
wp_reset_postdata();
?>
If you're on services page, so why do you use the 'publication-category' in
wp_get_object_terms(
$post->ID,
'publication-category',
array( 'fields' => 'ids' )
);
Seems like you have to use
$custom_taxterms = get_the_terms($post->ID, 'service-category');
$terms = [];
foreach ($custom_taxterms as $term) {
$terms[] = $term->slug
}
$args = array(
'post_type' => 'publication',
'post_status' => 'publish',
'posts_per_page' => 10,
'orderby' => 'rand',
'order' => 'ASC',
'tax_query' => array( array(
'taxonomy' => 'publication-category',
'field' => 'slug',
'terms' => $terms
)),
);
And create same slugs for both terms in both categories. From my understanding. It's quite difficult to understand your architecture
I find the solution by changing service-category taxonomy to publication-category as same with other post-type and creating relationship with below code from : https://wordpress.stackexchange.com/questions/139571/display-posts-with-same-taxonomy-term?rq=1
Thanks everyone
<?php
//get the post's venues
$custom_terms = wp_get_post_terms($post->ID, 'publication-category');
if( $custom_terms ){
// going to hold our tax_query params
$tax_query = array();
// add the relation parameter (not sure if it causes trouble if only 1 term so what the heck)
if( count( $custom_terms > 1 ) )
$tax_query['relation'] = 'OR' ;
// loop through venus and build a tax query
foreach( $custom_terms as $custom_term ) {
$tax_query[] = array(
'taxonomy' => 'publication-category',
'field' => 'slug',
'terms' => $custom_term->slug,
);
}
// put all the WP_Query args together
$args = array( 'post_type' => 'publication',
'posts_per_page' => 20,
'tax_query' => $tax_query );
// finally run the query
$loop = new WP_Query($args);
if( $loop->have_posts() ) {
while( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="listing-title"><?php the_title(); ?></div>
<div class="listing-image">
</div>
<?php
endwhile;
}
wp_reset_query();
}?>
With use of the Advanced Custom Fields plugin I created a select dropdown which contains 6 membership types. All of my 'listings' using this custom field are assigned one of the 6.
I have managed to get my listings to display in order of membership level, however it's not defining by category that your currently in. It's grabbing listings from every other category too.
<?php
// args
$args = array(
'numberposts' => -1,
'post_type' => 'directory_listings',
'meta_key' => 'membership_type',
'orderby' => 'meta_value',
'taxonomy' => 'listing_category'
);
// query
$wp_query = new WP_Query( $args )
?>
<?php if (have_posts()) : ?>
<?php
while( $wp_query->have_posts() ) {
the_post();
ldl_get_template_part('listing', 'compact');
ldl_get_featured_posts();
}
?>
<?php else : ?>
<?php endif; ?>
Also, I am using the plugin: https://wordpress.org/plugins/ldd-directory-lite/
WP_Query does not have a taxonomy parameter, you should use tax_query instead. More info in the Codex.
'tax_query' => array(
array(
'taxonomy' => 'listing_category',
'field' => 'slug',
'terms' => 'my-listing-category',
),
),
To grab the current taxonomy term dynamically (assuming you're on a listing_category taxonomy page):
'tax_query' => array(
array(
'taxonomy' => 'listing_category',
'field' => 'term_id',
'terms' => get_queried_object_id(),
),
),
I am trying to display a custom post type that has custom taxonomy, but I am not having any luck. Nothing is showing up. I appreciate any help.
Post type = gallery
Custom Taxonomy slug = photoarea
The 'photoarea' I want to display = fourth
<?php
$args = array(
'post_type' => 'gallery',
'tax_query' => array(
array(
'taxonomy' => 'photoarea',
'field' => 'fourth',
)
),
'posts_per_page' => 10,
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();
the_post_thumbnail();
endwhile; endif;
wp_reset_query();
?>
You may use below code snippet:
$the_query = new WP_Query( 'post_type=gallery&photoarea=fourth');
and then your while loop.
if my understanding is right, that you need to get the custom taxonomy with following code,
instead of field you must use term to get the posts in Fourth
<?php
$args = array(
'post_type' => 'gallery',
'tax_query' => array(
array(
'taxonomy' => 'photoarea',
'field' => 'slug',
'terms' => 'fourth'
)
),
'posts_per_page' => 10,
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();
the_post_thumbnail();
endwhile; endif;
wp_reset_query();
?>
$args = array('post_type' => 'gallery','posts_per_page'=>'-1','tax_query' => array(array(
'taxonomy' => 'photoarea',
'field' => 'term_id',
'terms' => $your_term_id,
),
),
);