• I want to list posts that are tagged in a custom taxonomy under headings that are tags in that taxonomy:

    Tag1
    * Post x
    * Post y

    Tag2
    * Post z

    Tag3

    I also want a list/index of the tags at the top of the page that are linked to each section, for quick access:

    Tag1 | Tag2 | Tag3… etc

    I’ve put together some code that works, but I see it makes a lot of database queries. I’m not a leet PHP programmer, and I can’t help feeling I’m not doing this the best way. Does anyone have any ideas to optimise it?

    (In the meantime, this code does work, so it is usable if anyone else wants to achieve the same thing. You can see it in action at https://nfcw.net/?p=2921 )

    <?php
    // output quick links list of countries
    $terms = get_terms('countries');
    if (count($terms)) {
    echo "<h3>Quick links</h3>";
    echo "<p style=\"text-align:center;\">";
    }
    $i=0; // counter for printing separator bars
    foreach ($terms as $term) {
    $wpq = array ('taxonomy'=>'countries','term'=>$term->slug);
    $query = new WP_Query ($wpq);
    $article_count = $query->post_count;
    echo "<a href=\"#".$term->slug."\">".$term->name."</a>";
    // output separator bar if not last item in list
    if ( $i < count($terms)-1 ) {
    echo " | " ;
    }
    $i++;
    }
    if (count($terms)) {
    echo "</p>";
    }
    foreach ($terms as $term) {
    $wpq = array ('taxonomy'=>'countries','term'=>$term->slug);
    $query = new WP_Query ($wpq);
    $article_count = $query->post_count;
    echo "<h3 class=\"term-heading\" id=\"".$term->slug."\">";
    echo $term->name;
    echo "</h3>";
    if ($article_count) {
    echo "<ul>";
    $posts = $query->posts;
    foreach ($posts as $post) {
    echo "<li><a href=\"".get_permalink()."\">".$post->post_title."</a> <span class=\"tiny\">".get_the_time('j M y')."</span></li>";
    }
    echo "</ul>";
    }
    echo "<p><a href=\"#top\">Back to top</a></p>";
    }
    ?>

    Cheers,

    M ??

Viewing 7 replies - 1 through 7 (of 7 total)
  • MichaelH

    (@michaelh)

    I believe you could replace this

    $wpq = array ('taxonomy'=>'countries','term'=>$term->slug);
    $query = new WP_Query ($wpq);
    $article_count = $query->post_count;

    with this

    $article_count = $term->count;

    Hey guys,

    I’d like this functionality but as the navigation in my sidebar. How would I go about doing that?

    Excellent.

    Thanks for sharing.

    I had to use the $post variable inside the get_permalink() function to get the correct links within a post (The Loop).

    MPClark – I could kiss you. This is exactly what I needed for a project I am working on. After 3 days of searching and a lot of trial and error, I have the code working as I need it. Thanks again. You are a life saver.

    @mpclark
    thanks! i wish i found this 2 days ago

    I could kiss you too. This is perfect! I’ve been hunting for something like this for days.

    i found the following to work very well… it is a slight combination of code shown on this post and of that at wordpress ticket #13582.

    the important difference is that it calls the taxonomy of a specific custom post_type.

    <?php
    $post_type = 'continents';
    $tax = 'countries';
    $tax_terms = get_terms($tax,'hide_empty=0');
    
    //list the taxonomy
    $i=0; // counter for printing separator bars
    foreach ($tax_terms as $tax_term) {
    $wpq = array ('taxonomy'=>$tax,'term'=>$tax_term->slug);
    $query = new WP_Query ($wpq);
    $article_count = $query->post_count;
    echo "<a href=\"#".$tax_term->slug."\">".$tax_term->name."</a>";
    // output separator bar if not last item in list
    if ( $i < count($tax_terms)-1 ) {
    echo " | " ;
    }
    $i++;
    }
    
    //list everything
    if ($tax_terms) {
      foreach ($tax_terms  as $tax_term) {
        $args=array(
          'post_type' => $post_type,
          "$tax" => $tax_term->slug,
          'post_status' => 'publish',
          'posts_per_page' => -1,
          'caller_get_posts'=> 1
        );
    
        $my_query = null;
        $my_query = new WP_Query($args);
        if( $my_query->have_posts() ) {
          echo "<h2 class=\"tax_term-heading\" id=\"".$tax_term->slug."\"> $tax_term->name </h2>";
          while ($my_query->have_posts()) : $my_query->the_post(); ?>
            <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
            <?php
          endwhile;
          echo "<p><a href=\"#top\">Back to top</a></p>";
        }
        wp_reset_query();
      }
    }
    ?>

    now in my application i took the code:
    $post_type = 'continents';
    and replaced it with:
    $post_type = $wp->query_vars["post_type"];
    which will call for the post_type of the template i am using.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘List posts by taxonomy tag’ is closed to new replies.