WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

According to a number of references online, I should be able to use the following function to add the class, has-children, to menu items that have children under them:

function gtp_nav_menu_css_class( $css_class, $item ) {
    global $wpdb;
    $has_children = $wpdb->get_var("SELECT COUNT(meta_id) FROM wp_postmeta WHERE meta_key='_menu_item_menu_item_parent' AND meta_value='" . $item->ID . "'");
    if ($has_children > 0) {
        array_push($css_class, 'has-children');
    return $css_class;
add_filter( 'nav_menu_css_class', 'gtp_nav_menu_css_class', 10, 4 );

I am using wp_nav_menu() in my header.php. However, after adding this code to functions.php, I am not seeing the has-children class being added to any menu items (when in fact there are menu items that contain children). Does anybody know what may be the culprit?


  • I am using WP 3.4.1.
  • I am running a Multisite setup
  • Table prefix is 'wp_'
  • Via debug echo statements, I can see that the function is getting executed, but not inside the conditional statement.
share|improve this question
what's your table prefix? – Milo Jul 23 '12 at 21:50
$table_prefix = 'wp_'; – Stephen Watkins Jul 23 '12 at 21:50
up vote 2 down vote accepted

You can't shouldn't hardcode table names into queries, especially with multisite. Swap $wpdb->postmeta for wp_postmeta and it'll likely work.

edit- technically you can, edited for clarity.

share|improve this answer
Wow, fantastic. – Stephen Watkins Jul 23 '12 at 22:04

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.