I am trying to get only top level term:
$cat_args = array( 'parent ' => 0, 'number' => 10, 'hide_empty' => false, ); $categories = get_terms( 'question_category' , $cat_args);
But this query return all childterms too, I tried everything but it always get child terms too.
I am trying since last 5 hours and can’t find whats wrong in my code, is this a WP bug or there is something wrong in my code ?
Thanks for helping.
Your code is correct, well almost correct. On first view, I must confess, I missed it too.
You have two syntax errors in your code. If you look closely, 'parent '
and 'parent'
is not the same. You should not leave blank spaces between single quotes ('
) and arguments.
Also, you don’t need to add a ,
after your last argument.
This should work
$cat_args = array( 'parent' => 0, 'number' => 10, 'hide_empty' => false );
EDIT 15-06-2016
As from WordPress 4.5, the syntax has changed for get_terms()
. The taxonomy
parameter is now included in the array or args, so the new use will look like this
$args = [ 'taxonomy' => 'my_tax', 'parent' => 0, 'number' => 10, 'hide_empty' => false ]; $terms = get_terms( $args );