• Hi.. i need to get the category ID by it’s name in the format: parent-category/category. For example: Images%2Fpaintings (images/paintings) and the get_cat_id or get_category_by_slug are not working in that case. I need to specify parent category as there are duplicate name of the categories.

    P.S. At the moment i’m getting the current category structure from the
    $query_string in the archive page.

    Thanks beforehand for any help…

Viewing 3 replies - 1 through 3 (of 3 total)
  • There may be a simpler way, but this should work:

    <?php
    global $wpdb;
    $comboname = 'images/paintings'; // parent/child category names
    $names = explode('/',$comboname);
    $sql = "SELECT child.*, parent.term_id parent_id, parent.name parent_name
    FROM $wpdb->terms child
    JOIN $wpdb->term_taxonomy childtt
       ON (child.term_id = childtt.term_id
           AND childtt.taxonomy = 'category')
    JOIN $wpdb->terms parent
       ON (parent.term_id = childtt.parent)
    JOIN $wpdb->term_taxonomy parenttt
       ON (parenttt.term_id = parent.term_id
           AND parenttt.taxonomy = 'category')
    WHERE 1=1
    AND child.name = '$names[1]'
    AND parent.name = '$names[0]'";
    $row = $wpdb->get_row($sql);
    $id = $row->term_id;
    ?>
    Thread Starter steeler

    (@steeler)

    thank you

    You are welcome. If this worked for you, please use the dropdown at top right to mark this topic ‘Resolved’.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Get category id by name ?’ is closed to new replies.