• I am customizing an old WordPress plugin in order to display on a single archive page all of the posts contained in the blog db except for those in the “Photo of the Day” category. I have successfully removed that category from the results (shown in the code below as “wp_term_relationships.term_taxonomy_id != 22”), but I am getting duplicate results for the other entries. (See page here.) I’d like to remove the duplicates.

    Here’s the code:

    <?php
    /*
    Plugin Name: SRG Clean Archives
    Plugin URI: https://www.sporadicnonsense.com
    Description: A nice uniform way to display your archives.
    Version: 2.1
    Author: Shawn Grimes
    Author URI: https://www.sporadicnonsense.com
    */
    
    function srg_clean_archives()
    {
        global $month, $wpdb, $wp_version;
        $now = current_time('mysql');
    
        if ( version_compare($wp_version, '2.1', '<') )
            $current_posts = "post_date < '$now'";
        else
            $current_posts = "post_type='post'";
    
        $arcresults = $wpdb->get_results("SELECT DISTINCT YEAR(post_date) AS year, MONTH(post_date) AS month, count(ID) as posts, term_taxonomy_id  FROM " . $wpdb->posts . ", wp_term_relationships WHERE post_status='publish' AND $current_posts AND post_password='' AND wp_term_relationships.object_id LIKE wp_posts.ID AND wp_term_relationships.term_taxonomy_id != 22 GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date DESC");
    
        if ($arcresults) {
            foreach ($arcresults as $arcresult) {
                $url = get_month_link($arcresult->year, $arcresult->month);
                $text = sprintf('%s %d', $month[zeroise($arcresult->month,2)], $arcresult->year);
                echo get_archives_link($url, $text, '','<div class="archivetitle">','</div>');
    
                $thismonth = zeroise($arcresult->month,2);
                $thisyear = $arcresult->year;
    
                $arcresults2 = $wpdb->get_results("SELECT DISTINCT ID, object_id, post_date, post_title, post_category, comment_status, term_taxonomy_id FROM wp_posts, wp_term_relationships WHERE post_date LIKE '$thisyear-$thismonth-%' AND $current_posts AND post_status='publish' AND post_password='' AND wp_term_relationships.object_id LIKE wp_posts.ID AND wp_term_relationships.term_taxonomy_id != 22 ORDER BY post_date DESC");
    
                if ($arcresults2) {
                    echo "\n";
                    foreach ($arcresults2 as $arcresult2) {
                           if ($arcresult2->post_date != '0000-00-00 00:00:00') {
                             $url       = get_permalink($arcresult2->ID);
                             $arc_title = $arcresult2->post_title;
    
                             if ($arc_title) $text = strip_tags($arc_title);
                            else $text = $arcresult2->ID;
                            $title_text = wp_specialchars($text, 1);
    
                              echo '<span class="date">' . mysql2date('d F Y', $arcresult2->post_date). "</span>\n<span class=\"commentauthor\">" . "<a href='$url' title='$title_text'>$text</a>" . "</span>";
    
                            // Here is where you can enable comment count if you would like, just remove the /* from the front and back of the line below
                            //Save the file and re-upload and you should have all your comment count again
                            /*$comments_count = $wpdb->get_var("SELECT COUNT(comment_id) FROM " . $wpdb->comments . " WHERE comment_post_ID=" . $arcresult2->ID . " AND comment_approved='1'");
                            if ($arcresult2->comment_status == "open" OR $comments_count > 0) echo ' (' . $comments_count . ')';*/
                             echo "
    
    \n";
                         }
                    }
                    echo "\n\n";
                }
            }
        }
    }
    ?>

    Thanks in advance to anyone who can help.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter jzahlaway

    (@jzahlaway)

    Thread Starter jzahlaway

    (@jzahlaway)

    I got it.

    I changed the SELECT call for $arcresults2 to:

    $arcresults2 = $wpdb->get_results("SELECT DISTINCT ID, object_id, post_date, post_title, post_category, comment_status FROM wp_posts, wp_term_relationships WHERE post_date LIKE '$thisyear-$thismonth-%' AND $current_posts AND post_status='publish' AND post_password='' AND wp_term_relationships.object_ID LIKE ID AND wp_term_relationships.term_taxonomy_id != 22 ORDER BY post_date DESC");

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Need help w/ removing duplicate results’ is closed to new replies.