0

I am creating a plugin (my first) which allows the site admin to display the most used post tags over the last X number of days. This plugin is an evolution of the WP Development question here - Display list of most used tags in the last 30 days.

Forgive me for not knowing the proper jargon.

I have most of the leg-work completed with the admin menu, etc, the only thing I have left is to actually display the results.

The following snippet of code works when I drop it into the single.php file.

<div class="recent-popular-tags">
<h3><?php echo $rpt_options['heading_tags']; ?></h3>
<?php $wpdb->show_errors(); ?> 
<?php
global $rpt_options;
$rpt_days = $rpt_options['number_days'];
global $wpdb;
$rpt_term_ids = $wpdb->get_col("
    SELECT term_id FROM $wpdb->term_taxonomy
    INNER JOIN $wpdb->term_relationships ON $wpdb->term_taxonomy.term_taxonomy_id=$wpdb->term_relationships.term_taxonomy_id
    INNER JOIN $wpdb->posts ON $wpdb->posts.ID = $wpdb->term_relationships.object_id
    WHERE DATE_SUB(CURDATE(), INTERVAL $rpt_days DAY) <= $wpdb->posts.post_date");

if(count($rpt_term_ids) > 0){

  $rpt_tags = get_tags(array(
    'orderby' => 'count',
    'order'   => 'DESC',
    'number'  => $rpt_options['number_tags'],
    'include' => $rpt_term_ids,
  ));
foreach ( (array) $rpt_tags as $rpt_tag ) {
echo '<span class="rpt-link"><a href="' . get_tag_link ($rpt_tag->term_id) . '" rel="tag">' . $rpt_tag->name . '</a></span>';
}
}
?>

http://i.imgur.com/UzzXPFk.png

I'm stuck at converting this code to a variable to append to $content.

Right now, I only have a very simply display function, to test that the number_days and number_tags settings will display the values entered into the settings page. They do.

function rpt_display_content ($content){

global $rpt_options;

if(is_single()){    

    $rpt_display = '<p>' . $rpt_options['rpt_tags_days'] . $rpt_options['rpt_tags_number'] . '</p>';    

    $content .= $rpt_display;
}

return $content;
}
add_filter('the_content', 'rpt_display_content');
0

You appear to be quite close to having this all sorted out. If you wrap your queries in a function, and separate the logic and presentation:

<?php 
function wpse_158425_get_terms(){
    global $wpdb, $rpt_options;

    $wpdb->show_errors();

    $rpt_days = $rpt_options['number_days'];

    $rpt_term_ids = $wpdb->get_col("
        SELECT term_id FROM $wpdb->term_taxonomy
        INNER JOIN $wpdb->term_relationships ON $wpdb->term_taxonomy.term_taxonomy_id = $wpdb->term_relationships.term_taxonomy_id
        INNER JOIN $wpdb->posts ON $wpdb->posts.ID = $wpdb->term_relationships.object_id
        WHERE DATE_SUB(CURDATE(), INTERVAL $rpt_days DAY) <= $wpdb->posts.post_date
        ");

        if(count($rpt_term_ids) > 0){

          $rpt_tags = get_tags(array(
            'orderby' => 'count',
            'order'   => 'DESC',
            'number'  => $rpt_options['number_tags'],
            'include' => $rpt_term_ids,
          ));

        } else {
            $rpt_tags = FALSE;
        }

        return $rpt_tags;

}

function wpse_158425_display_terms(){
    global $rpt_options;

    $tags = wpse_158425_get_terms();

    $recentTags = '<div class="recent-popular-tags">';

    $recentTags .= '<h3>'.$rpt_options['heading_tags'].'</h3>';

    if($tags != FALSE){
        foreach($tags as $tag){
           $recentTags .= '<span class="rpt-link"><a href="' . get_tag_link ($rpt_tag->term_id) . '" rel="tag">' . $rpt_tag->name . '</a></span>';
        }
    } else {
            $recentTags .= 'There are no popular tags right now.';
    }

    $recentTags .= '</div><!--.recent-popular-tags-->';

    return $recentTags;

}

function rpt_display_content ($content){

    if(is_single()){    

        $rpt_display = wpse_158425_display_terms();    

        $content .= $rpt_display;
    }

    return $content;
}
add_filter('the_content', 'rpt_display_content');

?>

You can further simplify this by making wpse_158245_get_terms() accept variables and passing the options in that manner, and setting default variables.

I did not have a chance to run this, you might run into some object/array errors, but you should be good to go.

2
  • Hi Aris, thanks for the reply. I have not tried it, because shortly after posting, I remembered a tip I had learned and figured out a solution - wordpress.stackexchange.com/a/154862/14558 - I am not sure which is better or why... Can you take a look and tell me which and why? Jul 17 '14 at 4:02
  • These solutions are extremely similar. Your solution solves one very specific problem, with one specific solution. The multi-function option solves for a variety of uses, and allows for a variety of outputs. The biggest single factor is that you could extend the multi-function way to put your terms into a transient (cached into the database), thereby increasing performance dramatically. You can do so with the single function, but it will become even more dense. Jul 17 '14 at 17:00
0

This is what I came up with and it works exactly as expected. Being a novice, is this a better solution than the solution posted by Aris Blevins? If not, why?

I honestly do not know.

<?php

/***** Display Functions *******/    
function rpt_display_content ($content){

global $rpt_options;

if(is_single()){    

    ob_start();

?>

<div class="recent-popular-tags">
<h3><?php echo $rpt_options['heading_tags']; ?></h3>
    <?php
    $rpt_days = $rpt_options['number_days'];
    global $wpdb;
    $rpt_term_ids = $wpdb->get_col("
        SELECT term_id FROM $wpdb->term_taxonomy
        INNER JOIN $wpdb->term_relationships ON $wpdb->term_taxonomy.term_taxonomy_id=$wpdb->term_relationships.term_taxonomy_id
        INNER JOIN $wpdb->posts ON $wpdb->posts.ID = $wpdb->term_relationships.object_id
        WHERE DATE_SUB(CURDATE(), INTERVAL $rpt_days DAY) <= $wpdb->posts.post_date");

    if(count($rpt_term_ids) > 0){

      $rpt_tags = get_tags(array(
        'orderby' => 'count',
        'order'   => 'DESC',
        'number'  => $rpt_options['number_tags'],
        'include' => $rpt_term_ids,
      ));
    foreach ( (array) $rpt_tags as $rpt_tag ) {
    echo '<span class="rpt-link"><a href="' . get_tag_link ($rpt_tag->term_id) . '" rel="tag">' . $rpt_tag->name . '</a></span>';
    }
    }
    ?>
</div>  

<?php

$content .= ob_get_clean();
}

return $content;
}

add_filter('the_content', 'rpt_display_content');
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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