Categories: Wordpress Development

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 3497 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.

Share
Published by
Mehedi Hasan

Recent Posts

WP Share A Friend – A Way to Capture Lead

What is Lead Capture: In general sense lead capture designates a process or way of…

5 years ago

Unlimited Modal POPUPS on MouseClick

Introduction Unlimited Modal POPUPS on MouseClick: Everyone knows about popup during site visits and most…

5 years ago

WordPress Pregnancy Calculator Plugin

Brief about Pregnancy Calculator : This WordPress Pregnancy Calculator plugin allows users to display an…

5 years ago

Interactive Service Plugin with Hover Effects VC

Introduction About this Service Plugin: This Visual Composer Service Plugin works as an addon to…

6 years ago

WP Dynamic Query String

Introduction of Dynamic Query String : This plugin allows user to set dynamic query string…

6 years ago

How to make scroll PDF in iFrame for iPAD Safari

There is an occasional bug for iPhone and iPad even they are designed as well and…

7 years ago