I'm trying to display all posts from taxonomy. I have posts type(Docs) and taxonomy (type_docs) I'm trying to list all posts from that post type. My code is like this but it is not working
<?php
$custom_terms = get_terms('type_docs');
foreach($custom_terms as $custom_term) {
wp_reset_query();
$args = array('post_type' => 'docs',
'tax_query' => array(
array(
'taxonomy' => 'type_docs',
'field' => 'slug',
'terms' => $custom_term->slug,
),
),
);
$loop = new WP_Query($args);
if($loop->have_posts()) {
echo '<h2>'.$custom_term->name.'</h2>';
while($loop->have_posts()) : $loop->the_post();
echo '<a href="'.get_permalink().'">'.get_the_title().'</a>
<br>';
endwhile;
}
}
?>
I'm trying to only list all posts from taxonomy, can somebody help with this?
I don't get exactly why you want to do thing like that but here is the solution:
<?php
// Get your custom terms
$custom_terms = get_terms('type_docs');
// Prepare the query
$args = array(
'post_type' => 'docs',
'posts_per_page' => -1,
'tax_query' => array()
);
// Prepare the array to receive terms tax_query
$terms_query = array();
foreach($custom_terms as $custom_term) {
// Add terms to the tax query
$args['tax_query'][] = array(
'taxonomy' => 'type_docs',
'field' => 'slug',
'terms' => $custom_term->slug,
);
}
// Get the posts (or you could do your new WP_Query)
$posts = get_posts($args);
// Display
foreach($posts as $post) : setup_postdata($post); ?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><br>
<?php endforeach; ?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.