How to list all the categories that have a certain word in title - first word of the category? in category.php

so, for example, if I am viewing the category "Green Apples Sells", it will list all the categories that start with the word "Green"

Update, added another check: IF the category contains one of these two words "South" or "North", then check if the next word is the same, and if it's not, don't list the category - only list the categories that match the first and the second word.

Example for the second check: I have categories for countries and continents: "South African Apples", "South African Oranges", "South Korean Apples", "South Korean Oranges"

now, when I browse the category 'South African Apples' I want to show the categories from South Africa (so it must check if the second word is the same, because in this case we have "South" in the title, which is one of the two words mentioned), not from South Korea, that's why if you check for the second word after "South", you will get only the right country

thank you


You can try this code below

$args = array(
  'orderby'           => 'name', 
  'order'             => 'ASC',
  'name__like'        => '%LIKE%',
  'description__like' => '%LIKE%',

$terms = get_terms( array( "taxonomy" ), $args );

See more in Codex

| improve this answer | |
  • 1
    thank you, but I am not sure how to use this. How do I pass the first word from the title of the category? that word changes, it's not static, as I have many categories. and then, how do I make sure it has no children...can you please add a more complete answer? – whitelord Mar 24 '15 at 15:02

You could do it for example like this:

function wpse182116_get_categories_starting_with($word) {
$categories = get_categories();
    if (!empty($categories)) {
        $relevant_ids = array();
        foreach($categories as $c) {
            $cat_name = $c->name;
            if(substr($cat_name, 0, strlen($word)) == $word) { $relevant_ids[] = $c->cat_ID; }
    $term_ids = implode(',' , $relevant_ids);
    $list = wp_list_categories( '&title_li=&style=none&echo=0&taxonomy=category&include=' . $term_ids );
    $list = rtrim(trim(str_replace('<br />',  ' |', $list)), ' |');

    return $list;

Put that in your functions.php and then in your archive.php you echo them out like

$title = single_cat_title('', false); echo wpse182116_get_categories_starting_with(strtok($title, " "));
edit: fixed per comments

You can of course change the styling in the function and what not. Now they are echoed like Green shoes | Green other stuff etc with links.

I have no idea if this is efficient or anything, just whipped it up.

EDIT: by waiting a second and seeing the other answer, I think you should go with the args and name__like. I didn't do the research to see that something like that existed, sorry :D

Also note that this only checks the word in the front of the category name, so Pants Green wouldn't be a match for the function I wrote.

| improve this answer | |
  • thank you, but how could I use a word, if actually I have more categories? I just gave an example...the first word changes, it must be taken from the current category itself – whitelord Mar 24 '15 at 15:05
  • On an archive.php page you can probably just use it lie echo wpse182116_get_categories_starting_with(explode(' ',trim($title))[0]); which will take the first word of the current category's name. edit: or wpse182116_get_categories_starting_with(strtok($title, " ")); should do the same. – jimihenrik Mar 24 '15 at 15:11
  • tried both, they all show a list with all the categories – whitelord Mar 24 '15 at 15:44
  • 1
    Welp oh sorry, put $title = single_cat_title('', false); before and then try again! – jimihenrik Mar 24 '15 at 16:01
  • great! it's working! thank you very much! I don't want to push it, but if it's not much of a trouble, any way to exclude categories including a word (this time a static word), simply exclude "sells" – whitelord Mar 24 '15 at 16:44

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.