Get Custom Posts for Custom Taxonomies & Terms

Taxonomies are basically a way of grouping data in WordPress. In WordPress, the most common default taxonomies are ‘categories’ or ‘tags’ those has the grouping posts and these are named & recorded in the WordPress database as ‘category’ and ‘post_tag’ respectively. Specific categories or tags are called terms.

For the query of Custom post type in WordPress and return posts based on these custom taxonomy names and terms, we can use ‘tax_query’ within get_posts() as indicated below:

 

<?php
$args = array(
	 'posts_per_page' => 10,
	 'orderby' => 'rand',
	 'post_type' => 'music',
	 'album' => 'hot',
	 'post_status' => 'publish'
);
$show_albums = get_posts( $args );
?>

In the above query we have shown the ‘music’ custom query post having custom taxonomy name ‘album’ & the terms names is ‘hot’. The below functions also produce the same output:

$args = array(
     'posts_per_page' => 10,
	 'orderby' => 'rand',
	 'post_type' => 'music',
	 'post_status' => 'publish'
	 'tax_query' => array(
		array(
			'taxonomy' => 'album',
			'field' => 'slug',
			'terms' => 'hot'
		)
	)
);
$show_album= get_posts( $args );

 

Now, if we want to display more or multi terms in ‘album’ taxonomy then we have to use array for the terms & must user operator for the query:

$args = array(
     'posts_per_page' => 10,
	 'orderby' => 'rand',
	 'post_type' => 'music',
	 'post_status' => 'publish'
	 'tax_query' => array(
		array(
			'taxonomy' => 'album',
			'field' => 'slug',
			'terms' => array('hot', 'cool'),//multi-terms used 
            'operator' => 'IN' 
		)
	)
);
$show_album= get_posts( $args );

The same above query can be followed by term id if you don’t know the slug. Just used below code then:

$args = array(
     'posts_per_page' => 10,
	 'orderby' => 'rand',
	 'post_type' => 'music',
	 'post_status' => 'publish'
	 'tax_query' => array(
		array(
			'taxonomy' => 'album',
			'field' => 'id',
			'terms' => array(5, 10),//multi-terms by id
            'operator' => 'IN' 
		)
	)
);
$show_album= get_posts( $args );

 

Now, we will show NOT IN operator where all the post will be displayed EXCEPT ‘hot’ & ‘cool’ terms. Follow the below code:

$args = array(
     'posts_per_page' => 10,
	 'orderby' => 'rand',
	 'post_type' => 'music',
	 'post_status' => 'publish'
	 'tax_query' => array(
		array(
			'taxonomy' => 'album',
			'field' => 'slug',
			'terms' => array('hot', 'cool'),//multi-terms used 
            'operator' => 'NOT IN' 
		)
	)
);
$show_album= get_posts( $args );

 

I think, so far you understand all the above code and hope can find your way to display custom taxonomy post from custom post type. In the final code i am going to show you how to display post from multi taxonomy:

$args = array(
     'posts_per_page' => 10,
	 'orderby' => 'rand',
	 'post_type' => 'music',
	 'post_status' => 'publish'
	 'tax_query' => array(
		array(
			'taxonomy' => 'album',
			'field' => 'slug',
			'terms' => array('hot', 'cool'),//multi-terms used 
                        'operator' => 'IN' 
		),
        array(
			'taxonomy' => 'photo-gallery',
			'field' => 'slug',
			'terms' => array('nature', 'sunset'),//multi-terms used 
            'operator' => 'NOT IN' 
		)
	)
);
$show_album= get_posts( $args );

Hope all the examples code above demonstrate nice solution for your theme/ plugin development. Still if you have question feel free to drop message below.

Thanks.

This post has already been read 3492 times!

Mehedi Hasan

Cool WordPress Developer having much agile experience to develop any kind of WordPress sites & plugins. Also good in troubleshooting, fixing & making any kind of tweaks for WP site.

More Posts

Mehedi Hasan

Cool WordPress Developer having much agile experience to develop any kind of WordPress sites & plugins. Also good in troubleshooting, fixing & making any kind of tweaks for WP site.

One thought on “Get Custom Posts for Custom Taxonomies & Terms

  • March 3, 2015 at 3:03 am
    Permalink

    Explore the best essay writing resources for high school students. . know plus writing prompts. ABC’s of the Writing Process Writing help from the beginning to .
    custom essay writing online
    24 Jan 2015 . Sat Essay Writing Help Novel The low unemployment that Hong Kong that exports have can indicate problems with the liver in turn increasing .

Comments are closed.