Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to get the terms of a taxonomy (kind) and the number of content they have but only for a specific category (get_parent_page_slug() = city name = category slug). This is what I have so far:

    $args = array(
    //          'taxonomy' => 'tipo',
                'hide_empty' => 1,
            );
    $terms = get_terms('kind', $args);

    //var_dump($terms);exit;

    $count = count($terms);
    echo '<ul>';
    echo '<li><a href="' . get_bloginfo('url') . '/' . get_parent_page_slug() . '/' . $pagename . '/' . '" title="' . sprintf(__('View all post filed under %s', 'my_localization_domain'), $term->name) . '">Todos (' . $count . ')</a></li>';
    foreach ($terms as $term) {
        echo '<li><a href="' . get_bloginfo('url') . '/' . get_parent_page_slug() . '/' . $pagename . '/?tipo=' . $term->slug . '" title="' . sprintf(__('View all post filed under %s', 'my_localization_domain'), $term->name) . '">' . $term->name . ' ('. $term->count .')</a></li>';
    }
    echo '<ul>';

Just in case I clarify: I'm doing this in a page template. The results I expect are these:

Accommodation in City Name

  • All (5)
  • Hotel (1)
  • Inn (2)
  • Log cabin (1)
  • Camping (1)

Thanks in advance

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

Well, I do not know if this is quite OK, but is returning the expected results ... so at the moment this it is enough for me.

function my_custom_get_terms( $my_tax, $my_category, $show_all = true ){
    global $wpdb;
    global $pagename;

    $query = "SELECT Count(wp_posts.ID) AS my_term_count, wp_term_taxonomy.term_taxonomy_id, wp_term_taxonomy.taxonomy, wp_terms.name, wp_terms.term_id, wp_terms.slug AS term_slug, wp_terms.term_group, wp_terms_1.slug
    FROM (((((wp_term_relationships INNER JOIN wp_term_taxonomy ON wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id) INNER JOIN wp_terms ON wp_term_taxonomy.term_id = wp_terms.term_id) INNER JOIN wp_posts ON wp_term_relationships.object_id = wp_posts.ID) INNER JOIN wp_term_relationships AS wp_term_relationships_1 ON wp_posts.ID = wp_term_relationships_1.object_id) INNER JOIN wp_term_taxonomy AS wp_term_taxonomy_1 ON wp_term_relationships_1.term_taxonomy_id = wp_term_taxonomy_1.term_taxonomy_id) INNER JOIN wp_terms AS wp_terms_1 ON wp_term_taxonomy_1.term_id = wp_terms_1.term_id
    GROUP BY wp_term_taxonomy.term_taxonomy_id, wp_term_taxonomy.taxonomy, wp_terms.name, wp_terms.term_id, wp_terms.slug, wp_terms.term_group, wp_terms_1.slug
    HAVING (((wp_term_taxonomy.taxonomy)='$my_tax') AND ((wp_terms_1.slug) = '$my_category'))
    ORDER BY Count(wp_posts.ID) DESC";

    $datos = $wpdb->get_results($query, OBJECT);
    echo "<ul>";
    $total = 0;
    foreach ( $datos as $dato ) {
        echo '<li><a href="' . get_bloginfo('url') . '/' . get_parent_page_slug() . '/' . $pagename . '/?tipo=' . $dato->term_slug . '">' . $dato->name . ' (' . $dato->my_term_count . ')</a></li>';
        $total += $dato->my_term_count;
    }

    if ( $show_all ){
        echo '<li><a href="' . get_bloginfo('url') . '/' . get_parent_page_slug() . '/' . $pagename . '/">Todos (' . $total . ')</a></li>';
    }
    echo "</ul>";
}



my_custom_get_terms( 'tipo', get_parent_page_slug(), true );

thanks anyway

share|improve this answer
add comment

Your Answer

 
discard

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.