Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all posts from custom taxonomy in Wordpress

Is there a way to get all the posts from a taxonomy in Wordpress ?

In taxonomy.php, I have this code that gets the posts from the term related to the current term.

$current_query = $wp_query->query_vars;
query_posts( array( $current_query['taxonomy'] => $current_query['term'], 'showposts' => 10 ) );

I'd like to create a page with all the posts in the taxonomy, regardless of the term.

Is there a simple way to do this, or do I have to query the taxonomy for the terms, then loop trough them, etc.

like image 872
Andrei Avatar asked Jul 28 '10 15:07

Andrei


People also ask

How do I show post by taxonomy in WordPress?

php // get taxonomies terms links function custom_taxonomies_terms_links() { global $post, $post_id; // get post by post id $post = &get_post($post->ID); // get post type by post $post_type = $post->post_type; // get post type taxonomies $taxonomies = get_object_taxonomies($post_type); $out = "<ul>"; foreach ($ ...

How do I get a custom taxonomy field in WordPress?

Customizing the HTML for a WordPress Taxonomy Term can be easily done by editing the category. php , tag. php or taxonomy. php file in your theme.

How do I query custom post type in WordPress?

You can query posts of a specific type by passing the post_type key in the arguments array of the WP_Query class constructor.

How do I display custom post category wise in WordPress?

Simply go to the Appearance » Widgets page and add the 'Latest Posts' block to your sidebar. By default, the block will show your most recent posts. You edit the block settings and scroll to the 'Sorting & Filtering' section. From here, you can choose the category that you want to display posts from.


1 Answers

@PaBLoX made a very nice solution but I made a solution myself what is little tricky and doesn't need to query for all the posts every time for each of the terms. and what if there are more than one term assigned in a single post? Won't it render same post multiple times?

 <?php
     $taxonomy = 'my_taxonomy'; // this is the name of the taxonomy
     $terms = get_terms($taxonomy);
     $args = array(
        'post_type' => 'post',
        'tax_query' => array(
                    array(
                        'taxonomy' => 'updates',
                        'field' => 'slug',
                        'terms' => wp_list_pluck($terms,'slug')
                    )
                )
        );

     $my_query = new WP_Query( $args );
     if($my_query->have_posts()) :
         while ($my_query->have_posts()) : $my_query->the_post();

              // do what you want to do with the queried posts

          endwhile;
     endif;
  ?>

wp_list_pluck

like image 97
maksbd19 Avatar answered Oct 07 '22 15:10

maksbd19