0

Currently I am able to get all categories but I want only few categories to display based on name or slug for example.

$categories = get_categories('accessibility','wcag', 'abc');

is this possible ?

  • What do you mean by "few categories to display"? Do you want posts from those categories, or just the term objects themselves? – Nathan Powell Jun 25 at 13:40
  • just term objects – Abhilash Narayan Jun 25 at 14:00
  • What criteria? You need to be specific. – Jacob Peattie Jun 25 at 14:04
1

If you are looking for specific term objects from the term slugs, from a specific taxonomy, I think new WP_Term_Query() is your best bet:

$term_args = array(
  'taxonomy' => 'category',
  'name' => array( 'accessibility','wcag', 'abc' )
  'hide_empty' => false,
  'fields' => 'all',
  'count' => true,
);

$term_query = new WP_Term_Query($term_args);

foreach($term_query->terms as $term){
    echo '<pre>';
    print_r($term); // You'll see the term object here, which is what I think you are after
    echo '</pre>';
}

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.