Skip to content

Instantly share code, notes, and snippets.

@manoj-singh-developer
Created March 13, 2019 12:28
Show Gist options
  • Select an option

  • Save manoj-singh-developer/5756bad1232c93489b26e791217cabd3 to your computer and use it in GitHub Desktop.

Select an option

Save manoj-singh-developer/5756bad1232c93489b26e791217cabd3 to your computer and use it in GitHub Desktop.
Taxonomies are basically a way of grouping data in WordPress. The most common default taxonomies used in WordPress are when grouping posts as either ‘categories’ or ‘tags’ and these are named and recorded in the WordPress database as ‘category’ and ‘post_tag’ respectively. Specific categories or tags are called terms.
Many plugins and theme functionality will use their own custom taxonomies to group data together in a meaningful way. A photo gallery for example might have a taxonomy name ‘gallery_cat’ with terms such as ‘Sunsets’, ‘Portraits’, ‘Nature’ etc. Each term has an associated name, slug and term ID.
To query WordPress and return posts based on these custom taxonomy names and terms, we can use ‘tax_query’ within get_posts() as indicated below:
01
02
03
04
05
06
07
08
09
10
11
<?php
get_posts(array(
'post_type' => 'gallery',
'tax_query' => array(
array(
'taxonomy' => 'gallery_cat',
'field' => 'term_id',
'terms' => 45)
))
);
?>
The ‘tax_query’ array takes 3 required arguments:
‘taxonomy’- the slug that is used to identify the taxonomy.
‘field’ – can be either ‘term_id’ (the numeric ID of the term), ‘slug’ or ‘name’ (the term name such as ‘Sunsets’)
‘terms’ – will need to be the ID value, slug or name depending what ‘field’ is set to.
Here’s another example which will select all posts with the terms ‘Sunsets’ and ‘Nature’, based on the same custom taxonomy. As you can see, to select multiple terms from the custom taxonomy, we need to put them as a comma seperated string within an array.
01
02
03
04
05
06
07
08
09
10
11
12
13
14
<?php
get_posts(array(
'showposts' => -1,
'post_type' => 'gallery',
'tax_query' => array(
array(
'taxonomy' => 'gallery_cat',
'field' => 'name',
'terms' => array('Sunsets', 'Nature'))
),
'orderby' => 'title',
'order' => 'ASC')
);
?>
We have set ‘showposts’ to -1 so that all posts will be returned and the posts will be ordered alphabetically by title.
If you are unsure of what values should go into post_type, taxonomy and terms, the easiest way to find out is to select the custom taxonomy and one of the terms within the WordPress admin and look at the URL at the top, as shown in the below.
Custom Taxonomy URL sample
Finally, we can put all this in a simple loop to display our content, title and ID of all posts belonging to our chosen terms.
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
<?php
$myposts = get_posts(array(
'showposts' => -1,
'post_type' => 'gallery',
'tax_query' => array(
array(
'taxonomy' => 'gallery_cat',
'field' => 'slug',
'terms' => array('sunsets', 'nature'))
))
);
foreach ($myposts as $mypost) {
echo $mypost->post_title . '<br/>';
echo $mypost->post_content . '<br/>';
echo $mypost->ID . '<br/><br/>';
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment