0

hello i have post_type "product" and taxonomy "price" can someone help me to query that custom post type with the selected taxonomy?

my current code is like this

<?php $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
$args = array(
 'post_type'   => 'product',
 'posts_per_page' => 5,
 'tax_query' => array(
    array(
      'taxonomy' => 'productcategories',
      'terms' => $term->name
    )
  )
 );
 query_posts($args);
 print_r($args);
 ?>

but nothing on result, can someone help me? thank you

  • your taxonomy is price but you are querying productcategories? – Milo Mar 4 '15 at 2:04
  • my taxonomy is productcategories it like categories on normal post and i have several on taxonomy productcategories, price, area, etc this is like i have category price on normal post. i need to query depends on that productcategories term name on specific pages @Milo – Welyanto Mar 4 '15 at 2:10
  • Never ever use query_posts. Use WP_Query instead – Pieter Goosen Mar 4 '15 at 8:54
0

change your $args array to the following

$args = array(
    'post_type'   => 'product',
    'posts_per_page' => 5,
    'tax_query' => array(
        array(
            'taxonomy' => 'productcategories',
            'terms'    => $term->name,
            'field'    => 'name'

        ),
    ),
);
0
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );

$args = array(
    'post_type'   => 'product',
    'posts_per_page' => 5,
    'tax_query' => array(
        array(
            'taxonomy' => 'price',
            'terms'    => $term->name,
            'field'    => 'name'

        ),
    ),
);

$allproducts = new WP_Query($args);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.