16

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.

2
  • I think you mean only top level terms from your taxonomy question_category Aug 14 '14 at 11:39
  • Yes I mean that
    – Aryan
    Aug 14 '14 at 11:40
24

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 );
6
  • 2
    Just a note: you don't need to add , after last array argument, HOWEVER, it is a good practice to do so ;)
    – jave.web
    Jun 15 '16 at 9:02
  • @jave.web Thanks for that, I know that, I just don't always do that. ;-) Jun 15 '16 at 9:05
  • @jave.web It's actually not good practice to add a , after the last array argument, as this will break other languages which arent as tolerant to redundant commas. Eg. JSON will break if you do this.
    – Mikepote
    Apr 16 '19 at 14:20
  • 3
    @Mikepote that is actually not true, it is good practise and if you are trying to move code from one language to another, you should use proper conversion/ter. It is like saying you can't have object creations and/or variables as arguments, because if you copy/paste it breaks JSON... It is good practise even in JavaScript, therefore the support was added later on everywhere. Copy/paste inside the same language and project is much more probable and adding the trailing comma if possible prevents common slow-downers - that is not only from my experience, but also from experience of others.
    – jave.web
    Apr 17 '19 at 21:26
  • @Mikepote To tack on to what jave.web is saying, it's not only good practice but specifically recommended in the WordPress PHP Coding Standards, which ideally all WordPress-specific code should follow: make.wordpress.org/core/handbook/best-practices/… May 3 '19 at 17:29

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.