I've written a shortcode which displays 'x' number of posts on the homepage of my site when I write [blog posts_per_page-"3"]. Here it is:

function getBlogPosts($atts) {

    "posts_per_page" => '',
  ), $atts));

  $queryArgs = array(
    "posts_per_page" => $posts_per_page

  $queryPosts = new WP_Query($queryArgs);
  $output = "";

  $output .= "<ul class='articles-list clearfix'>";

    if ( $queryPosts->have_posts() ) : while ( $queryPosts->have_posts() ) : $queryPosts->the_post();
      $output .= "<li class='clearfix ind-article'>
        <a class='article-container' href='". get_the_permalink() ."'>";
        if (has_post_thumbnail($post->ID)) {
            $output .= get_the_post_thumbnail($post->ID, 'medium'); // medium img size = 300x300. => make sure photos uploaded are at least 600x600
        $output .= "<div class='article-content'>";
          $output .= "<h3 class='entry-title gamma uppercase'>". get_the_title() ."</h3>";
          $output .= "<span class='entry-meta'>". get_the_date('l jS F') ."</span>";
      $output .= "</div></a></li>";
    endwhile; endif;

  $output .= "</ul>";

  return $output;
} add_shortcode('blog', 'getBlogPosts');

However, when I do this, a comment form for the blog post is also displayed (it's definitely the post's comment form, not the page - also seems to only be one comment form, for the last blog post displayed I think). I want to keep comments enabled but remove the comment from when the shortcode is used. How would I do this?

1 Answer 1


The problem

You must remember to call the core function wp_reset_postdata(), after your while loop, to restore the global $post object. The comment form is relying on that object, that you override with your $queryPosts->the_post() call.

Note that the extract() isn't recommended, check this answer by @toscho, for example.

Removing comments

To remove the comment form, when using a shortcode, you could check out my answer here.

If you want to remove the list of comments and the comment-form, after the shortcode, then you can try out one the following methods, within the shortcode's callback:

Method #1 Remove the queried comments via filters:

if( is_singular() && post_type_supports( get_post_type(), 'comments' ) )
    // Remove the comment form
    add_filter( 'comments_open', '__return_false' ); 

    // Remove the list of comments
    add_filter( 'comments_array', '__return_empty_array' );

Method #2 Force get_comments() to return an empty array:

if( is_singular() && post_type_supports( get_post_type(), 'comments' ) )
    // Remove the comment form
    add_filter( 'comments_open', '__return_false' );

    // Empty comments SQL query - Run only once
    add_filter( 'comments_clauses', function( $clauses )
        static $count = 0;
        if( 0 === $count++ )
            $clauses['where'] .= ' AND 1=0 ';
        return $clauses;

Here we run the filter callback only once, to prevent this from e.g. a recent comments widget in the sidebar.

Method #3 Modify the comment template via filter. Create an empty file comments-empty.php within your theme and use:

if( is_singular() && post_type_supports( get_post_type(), 'comments' ) )
    // Modify the comment template
    add_filter( 'comments_template', function( $template )   
        if( $tmp = locate_template( 'comments-empty.php' ) )
            $template = $tmp;
        return $template;
    } );

You can modify the file path to your needs.

Method #4 Hide it with CSS. Example:

if( is_singular() && post_type_supports( get_post_type(), 'comments' ) )
    print '<style> #comments { display: none } </style>';
  • Hi, thanks for the answer and many different ways of doing it! I used method 1 which seemed to work. Also thanks for the tips about wp_reset_postdata() (I always forget it) and the extract() stuff. Thanks!
    – Tom Oakley
    Commented Jul 17, 2015 at 8:47
  • You're welcome. I just fixed a typo in method #1. @TomOakley
    – birgire
    Commented Jul 17, 2015 at 9:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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