Change site style→

style-1 style-2
  1. nazenani
  2. >
  3. >
  4. >
  5. Get taxonomy data.

Get taxonomy data.

Taxonomy is category in wordpress, but don’t equal “category” in post.
!Refer Theory: Post type and taxonomy!

The code below show how to get taxonomy data. “tax_query” was used in the script.

$slug=get_query_var(‘term’);
$term=get_term_by(‘slug’,$slug,’works_tax’);

“get_query_var()” gets data in WP_Query. “term” is information in taxonomy.
get_term_by() gets all Term data from database.

$slug has all taxonomy information, $term gets $slug(value) of slug(field) in works_tax(taxonomy).

$args=array(
‘post_type’=>’works’,
‘post_status’=>’publish’,
‘posts_per_page’=>10,
‘paged’=>$paged,
‘tax_query’=>array(
array(
‘taxonomy’=>’works_tax’,
‘terms’=>array($slug),
‘field’=>’slug’,
‘operation’=>’IN’,
),
),
);

‘tax_query’ is a parameter using to get data interacting WP_Query. It has some keys and values in array and filter data which will get in WP_Query.

  • ‘taxonomy’: set taxonomy.
  • ‘terms’:taxonomy terms.
  • ‘field’: data source field in taxonomy.
  • ‘operation’: operator to test. IN=include.

Reference

WordPress codex:Class Reference/WP_Query
query_posts(WP_Queryクラス)で’tax_query’を使いこなす

comment

Fields with * is required.

go to related tags archive?