Search code examples
foreachresetshortcodewordpress

How to Reset this WP Query


I built this shortcode function for Wordpress, it lists all posts with comments status open of a specific category.

I use it on a page to make a sort of summary of open discussion on my blog. It works but after I use it the global wo query of my wordpress page is changed by the last post listed by my shortcode.

for example the "edit" link at the bottom of the page takes me to edit the last post listed by the shortcode instead of the page I'm on.

I used the standard wp_reset_query(); function to fix it, but it's not working... any idea?

this is the code:

1) Select all posts with comment_status = $status

function get_posts_based_on_comment_status($categories, $status='open', $limit=100) {
       global $wpdb;
       $sql = "SELECT * FROM $wpdb->posts
              LEFT JOIN $wpdb->term_relationships
              ON($wpdb->posts.ID=$wpdb->term_relationships.object_id)
              LEFT JOIN $wpdb->term_taxonomy
              ON($wpdb->term_relationships.term_taxonomy_id=$wpdb->term_taxonomy.term_taxonomy_id)
              WHERE $wpdb->term_taxonomy.term_id='$categories'
              AND $wpdb->term_taxonomy.taxonomy='category'
              AND $wpdb->posts.post_status='publish'
              AND $wpdb->posts.comment_status='$status'
              ORDER BY $wpdb->posts.post_date DESC
              LIMIT $limit;";
       $results = $wpdb->get_results($sql);
       if (!empty($results)) return $results;
       else return FALSE;
    }

2) Building the loop (with the Reset in the end)

function print_posts_based_on_comments($atts) {
        $render = '';
         extract(shortcode_atts(array(
                'cat' => '',
                'title' => '',
                'comment' => 'open',
                'limit' => 10
            ), $atts));
        $render .= '<div class="forum_post_list">';

        if ($cat){      
                $block_title = ($title != '')? $title : get_cat_name( $cat ) ;  
                if ($block_title != 'no'){
                    $render .= '<h3>'.$block_title.'</h3>'; 
                    }
                $posts_c = get_posts_based_on_comment_status($cat, $comment, $limit);
                if ($posts_c) {
                    global $post;
                    $render .= '<ul>';
                    foreach ($posts_c as $post) :
                        setup_postdata($post); 
                        $comment_count = get_comments_number( get_the_ID() ); 
                        $render .= '<li>';
                        $render .= '<h5>';
                        $render .= '<a class="title" href="'. get_permalink().'">'. get_the_title().'</a> ';
                        $render .= '<a class="comments" href="'. get_permalink().'#comment-wrap">'.$comment_count.'<span>Go to Discussion</span></a>';
                        $render .= '</h5>';
                        $render .= '</li>';
                    endforeach;
                    $render .= '</ul>';
                }
        } else{
                $render .= '<h5>'.__('You Must Specify at least one category. Ex: [forum cat=1]', 'smallscalefarming').'</h5>';
        }
        $render .= '</div>';    
        return $render; 
        wp_reset_query();
}

3) Shortcode it

 add_shortcode('forum', 'print_posts_based_on_comments');  

Solution

  • Problem solved, The correct function to use in my code is wp_reset_postdata(); before the end of the foreach loop, this is working:

    function print_posts_based_on_comments($atts) {
            $render = '';
             extract(shortcode_atts(array(
                    'cat' => '',
                    'title' => '',
                    'comment' => 'open',
                    'limit' => 10
                ), $atts));
            $render .= '<div class="forum_post_list">';
    
            if ($cat){      
                    $block_title = ($title != '')? $title : get_cat_name( $cat ) ;  
                    if ($block_title != 'no'){
                        $render .= '<h3>'.$block_title.'</h3>'; 
                        }
                    $posts_c = get_posts_based_on_comment_status($cat, $comment, $limit);
                    if ($posts_c) {
                        global $post;
                        $render .= '<ul>';
                        foreach ($posts_c as $post) :
                            setup_postdata($post); 
                            $comment_count = get_comments_number( get_the_ID() ); 
                            $render .= '<li>';
                            $render .= '<h5>';
                            $render .= '<a class="title" href="'. get_permalink().'">'. get_the_title().'</a> ';
                            $render .= '<a class="comments" href="'. get_permalink().'#comment-wrap">'.$comment_count.'<span>Go to Discussion</span></a>';
                            $render .= '</h5>';
                            $render .= '</li>';
                            wp_reset_postdata();
                        endforeach;
                        $render .= '</ul>';
                    }
            } else{
                    $render .= '<h5>'.__('You Must Specify at least one category. Ex: [forum cat=1]', 'smallscalefarming').'</h5>';
            }
            $render .= '</div>';    
            return $render; 
    }