• Resolved sw4mp

    (@sw4mp)


    Let’s say I have a specific list of tags associated to posts that I want to point out, for example: Superman, batman, Chicago. So now let’s say I go to look at some random post in my wordpress with the tag “Superman” I might notice it also has the tag “Red Cape.”

    What I’m wanting is to take my list of “Superman, batman, Chicago”, find all the posts that have these specific tags, then display only the tags that share these tags (so for example “Red Cape”), give them a custom link, display them in a random order, and limit how many can be displayed at a time.

    So, let’s say I have:
    Post 1 with tags Superman, Red Cape
    Post 2 with tags Batman
    Post 3 with tags Chicago, Life
    Post 4 with tags Happy Meal

    What I want is some form of php that would return something like (in a random order every refresh and limited to a certain amount of tags to be displayed):
    Red Cape, Life

    (Notice, “Happy Meal” wasn’t displayed because it wasn’t a part of any posts with the original set of tags I wanted to query)

    I hope this doesn’t sound as complicated as I think I’m making it sound, thank you!

Viewing 5 replies - 1 through 5 (of 5 total)
  • Well, it isn’t really simple, but I think this is close to what you want:

    $tag_names = array( 'Superman', 'Batman', 'Chicago' );
    $tag_name_list = "'" . join("', '", $tag_names) . "'";
    
    $sql = "
    SELECT DISTINCT tt.*, t.*
    FROM $wpdb->term_relationships tr
    JOIN $wpdb->term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
    JOIN $wpdb->terms t ON t.term_id = tt.term_id
    JOIN $wpdb->posts p ON tr.object_id = p.ID
    WHERE tr.object_id IN (
            SELECT ID
            FROM $wpdb->posts p2
            JOIN $wpdb->term_relationships tr2 ON p2.ID = tr2.object_id
            JOIN $wpdb->term_taxonomy tt2 ON tr2.term_taxonomy_id = tt2.term_taxonomy_id
            JOIN $wpdb->terms t2 ON tt2.term_id = t2.term_id
            WHERE p2.post_type = 'post'
            AND p2.post_status = 'publish'
            AND tt2.taxonomy = 'post_tag'
            AND t2.name IN ($tag_name_list)
       )
    AND tt.taxonomy = 'post_tag'
    AND t.name NOT IN ($tag_name_list)
    ORDER BY t.name ASC
    ";
    $rows = $wpdb->get_results($sql);
    
    $sep = '';
    foreach ($rows as $row) {
       echo $sep;
       $sep = ', '; ?>
       <a href="<?php echo get_tag_link($row->term_taxonomy_id); ?>"><?php echo $row->name; ?></a>
    <?php
    }

    Couple of small changes, I forgot the requirement to limit the number and randomize:

    $tag_names = array( 'Superman', 'Batman', 'Chicago' );
    $tag_name_list = "'" . join("', '", $tag_names) . "'";
    $tag_limit = 3;  // Number of tags to show
    
    $sql = "
    SELECT DISTINCT tt.*, t.*
    FROM $wpdb->term_relationships tr
    JOIN $wpdb->term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
    JOIN $wpdb->terms t ON t.term_id = tt.term_id
    JOIN $wpdb->posts p ON tr.object_id = p.ID
    WHERE tr.object_id IN (
            SELECT DISTINCT ID
            FROM $wpdb->posts p2
            JOIN $wpdb->term_relationships tr2 ON p2.ID = tr2.object_id
            JOIN $wpdb->term_taxonomy tt2 ON tr2.term_taxonomy_id = tt2.term_taxonomy_id
            JOIN $wpdb->terms t2 ON tt2.term_id = t2.term_id
            WHERE p2.post_type = 'post'
            AND p2.post_status = 'publish'
            AND tt2.taxonomy = 'post_tag'
            AND t2.name IN ($tag_name_list)
       )
    AND tt.taxonomy = 'post_tag'
    AND t.name NOT IN ($tag_name_list)
    ORDER BY RAND() LIMIT 0,$tag_limit
    ";
    $rows = $wpdb->get_results($sql);
    
    $sep = '';
    foreach ($rows as $row) {
       echo $sep;
       $sep = ', '; ?>
       <a href="<?php echo get_tag_link($row->term_taxonomy_id); ?>"><?php echo $row->name; ?></a>
    <?php
    }
    Thread Starter sw4mp

    (@sw4mp)

    This worked perfectly. Thank you so much! If there is anything I can do to help you just let me know.

    If your problem has been solved, please mark this topic ‘Resolved’.

    Thread Starter sw4mp

    (@sw4mp)

    Resolved.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Display tags from tags’ is closed to new replies.