I have created a custom taxonomy for a custom post. Now i want to show all post related to a particular term. Suppose i have two terms term1 and term2. When click on term1 then all post related to term1 will show and post , that is related to term2 will not show. But now when i click term1 then post related to term1 and term2 are showing at a time. I have wrote the following code to taxonomy.php template.
<div class="main-content">
<?php
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
$the_query = new WP_Query( array(
'post_type' => array('newsbox_post'),
'tax_query' => array(
'taxonomy' => $term->taxonomy,
'field' => 'slug',
'terms' => $term->name,
),
) );
?>
<?php if ($the_query->have_posts()) : while ($the_query->have_posts()) : $the_query->the_post(); ?>
<div class="post">
<?php the_title(); ?>
<?php the_content(); ?>
<?php echo get_the_term_list( $post->ID, $term->taxonomy, 'People: ', ', ' ); ?>
<hr />
</div>
<?php
endwhile;
wp_reset_postdata();
else:?>
<h3><?php _e('404 Error: Not Found'); ?></h3>
<?php
endif;
?>
</div>
Please tell me , how can i achieve that.
I have got the answer .
$the_query = new WP_Query( array(
'post_type' => array('newsbox_post'),
'tax_query' => array(
array(
'taxonomy' => $term->taxonomy,
'field' => 'slug',
'terms' => $term->slug,
),
),
) );
You should use slug instead of name like following code:-
$the_query = new WP_Query( array(
'post_type' => array('newsbox_post'),
'tax_query' => array(
array(
'taxonomy' => $term->taxonomy,
'field' => 'slug',
'terms' => $term->slug,
),
),
) );
Hope this will help you.
Related
I can't seem to work out why my ACF fields will not display below my woo-commerce products on the featured page when they display fine above them?
Products
<div id="post-load" class="row products">
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'nopaging' => true,
'tax_query' => array(
array(
'taxonomy' => 'product_visibility',
'field' => 'name',
'terms' => 'featured',
'operator' => 'IN'
),
),
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
global $product; ?>
<?php wc_get_template_part( 'content', 'product' ); ?>
<?php endwhile; ?>
</div>
ACF Example (This works fine if placed above the products)
<?php
$features = get_field('features');
if( $features ): ?>
<div class="col-md-12"><h3 class="txtc"><?php echo esc_html( $features['title'] ); ?></h3></div>
<?php endif; ?>
I have a custom post type called Developments. They are buildings. They have two different taxonomies: City and Status. On the City taxonomy page for one of the buildings I'm trying to show all posts associated with the Status taxonomy.
<?php
// Vars
$status_taxonomy = 'development_status';
$devs = get_terms( array(
'taxonomy' => $status_taxonomy,
'hide_empty' => true,
) );
?>
<?php
foreach($devs as $dev) :
$dev_id = $dev->term_id;
$dev_name = $dev->name;
?>
<?php
$term_slug = $dev->slug;
$dev_posts = new WP_Query( array(
'post_type' => 'developments',
'posts_per_page' => -1, //important for a PHP memory limit warning
'tax_query' => array(
array(
'taxonomy' => $status_taxonomy,
'field' => 'slug',
'terms' => $term_slug,
'operator' => 'IN',
),
),
));
if( $dev_posts->have_posts() ) :
echo '<h3>'. $dev_name .'</h3>';
while ( $dev_posts->have_posts() ) : $dev_posts->the_post();
?>
<div class="col-xs-12">
<h3>$dev_name</h3>
</div>
<div class="col-md-4">
<h4><?php the_title(); ?></h4>
</div>
<?php
endwhile;
endif;
wp_reset_postdata();
?>
<?php
endforeach;
?>
This code outputs all of the Status terms, but it's showing all the posts too, I need it to show posts that are associated to City. I am not sure how to modify the code above to achieve this.
Get your current taxonomy and term on the City taxonomy page like this:
$current_term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
And then change you tax_query to add another array so that the current city taxonomy is added to the restrictions:
'tax_query' => array
(
'relation' => 'AND',
array(
'taxonomy' => $status_taxonomy,
'field' => 'slug',
'terms' => $term_slug,
'operator' => 'IN',
),
array(
'taxonomy' => 'city',
'field' => 'slug',
'terms' => $current_term->slug,
'operator' => 'IN',
),
),
I have been trying to list my custom type 'movies' with the custom taxonomy genre with a value of drama.
I have tried several solutions but as yet I have failed.
<?php
$args = array(
'post_type' => 'movies',
'tax_query' => array(
array(
'taxonomy' => 'genre',
'field' => 'term_id',
'terms' => 'drama'
)
)
);
$drama = new WP_Query( $args );
if ( $drama->have_posts() ) : while ( $drama->have_posts() ) : $drama->the_post(); ?>
<h4><?php echo the_title(); ?></h4>
<?php endwhile; ?>
<?php else : echo '<p>NO CONTENT FOUND</p>'; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
Hope someone can shed some light on this matter.
Steven
The field parameter is wrong, in your case you need to set it to slug as drama is not a term_id
$args = array(
'post_type' => 'movies',
'tax_query' => array(
array(
'taxonomy' => 'genre',
'field' => 'slug',
'terms' => 'drama'
)
)
);
Hope it works !
IN my wp site, I have 2 category and a few post like that..
cat_1- post 1, post 2, post 3.
cat_2- post 2, post 3, post 4.
When i am in post 3 page, i want to show releted article only from category 2.here is my code:but it returns empty. Probably i did not catch the logic. Any help would be highly appreciated.
<?php
$terms = get_the_terms( $post_id, 'category' );
if( empty( $terms ) ) $terms = array();
$term_list = wp_list_pluck( $terms, 'slug' );
$related_args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'post_status' => 'publish',
'post__not_in' => array( get_the_ID() ),
'orderby' => 'desc',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => $term_list
),
array(
'taxonomy' => 'category',
'terms' => array('cat_1'),
'field' => 'slug',
'operator' => 'NOT IN',
),
),
);
$related = new WP_Query( $related_args );
if( $related->have_posts() ):
?>
<div class="post-navigation">
<h3>Related posts</h3>
<ul>
<?php while( $related->have_posts() ): $related->the_post(); ?>
<li><?php the_title(); ?></li>
<?php endwhile; ?>
</ul>
</div>
<?php
endif;
wp_reset_postdata();
?>
here is the following query ...may be helpful to you...
$custom_tex=array(array('taxonomy'=>'category','field'=>'slug','terms'=>'cat_2'));
$new = new WP_Query(array('post_type'=>'post','posts_per_page'=>-1, 'tax_query'=>$custom_tex,'order' => 'DESC','orderby' => 'ID' ));
while ($new->have_posts()) : $new->the_post();
echo $post->post_title;
echo "<br>";
endwhile;
Following is my wordpress query which I am using to display all the posts but the query is not displaying the post's tags, Also kindly let me know how to modify the following query so it displays the posts from any specific category.
<?php
$wp_query2 = null;
$wp_query2 = new WP_Query(array(
'post_type' => 'post',
'post_status' => 'publish',
'caller_get_posts'=> 0 ));
while ($wp_query2->have_posts()) : $wp_query2->the_post();
?>
<?php the_date(); ?>
<br />
<?php the_title(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
<?php
wp_reset_query();
?>
You may try this
$args = array(
'post_type' => 'post',
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => array( 'category1', 'category2' ) // replace these
),
array(
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => array( 'tag1, tag2' ) // replace these
)
)
);
$query = new WP_Query( $args );
while ($query->have_posts()) : $query->the_post();
// ...
the_content();
the_tags(); // display tags
endwhile;
Check WP Query and the_tags.