0

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

1 Answer 1

1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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