1

I am creating a custom post type and I added the comment option for it. Now all the comments submitted in that post type are also added to the general comment section.

How can I make sure the comment for a specific post type are separated from the general comments in the dashboard area?

2 Answers 2

4

All comments are just listed under "Comments" there is no attribute or parameter which creates a seperate "Comments" section/page for your custom post-type. Atleast not that I know of.

However, you could add a filter to the comments list, to filter all comments based on post-types.
It will be just like a category filter in the post list.

So you can create a HTML select field, which contains an option for every custom post-type. The value of each option needs to be the slug of the post-type. The select element needs a name of "post_type" to make the filtering work.

/**
 * Create custom comments filter for post types
 **/
function my_comments_filter() {

    // we are only getting the none-default post types here (no post, no page, no attachment)
    // you can also change this, just take a look at the get_post_types() function
    $args = array(
        'public'   => true,
        '_builtin' => false
    );
    $post_types = get_post_types( $args, 'objects' ); // we get the post types as objects

    if ($post_types) { // only start if there are custom post types 

        // make sure the name of the select field is called "post_type"
        echo '<select name="post_type" id="filter-by-post-type">';

        // I also add an empty option to reset the filtering and show all comments of all post-types
        echo '<option value="">'.__('All post types', 'my-textdomain').'</option>';

        // for each post-type that is found, we will create a new <option>
        foreach ($post_types as $post_type) {

            $label = $post_type->label; // get the label of the post-type
            $name = $post_type->name; // get the name(slug) of the post-type

            // value of the optionsfield is the name(slug) of the post-type
            echo '<option value="'.$name.'">'.$label.'</option>';

        }
        echo '</select>';
    } 

}
// we add our action to the 'restrict_manage_comments' hook so that our new select field get listet at the comments page
add_action( 'restrict_manage_comments', 'my_comments_filter' );

If you add this code to your functions.php, or better, to a plugin, you will see a new filter on the top of the comments backend. Here you can select a custom post-type and than click on Filter, to filter the comments. If you select "All post types" and click filter again, all comments should show.

1
  • Excellent! It works! The only problem, in my case, is that it doesn't detect the default post type "post". But I solved this by adding the echo '<option value="post">Posts</option>'; after foreach loop.
    – Yuri
    Commented Apr 1, 2023 at 0:30
0

Comments need to belong to a post $id. Check your code to make sure that the comment-saving/processing code is referencing the current post ID.

2
  • There is no "current post ID" in the dashboard. There is a dashboard widget listing all comments. That's what the OP is talking about.
    – fuxia
    Commented Jun 30, 2017 at 3:07
  • My read of the OP was that he was coding somewhere for the custom post type, and my conclusion was that his code needed to ensure that the post ID was part of the function that saves the comment. But (looking more closely at the OP) if the OP is looking for a 'filter' on the comments, then he will need to find/code a plugin that extends the filtering capabilities of the admin comment list page. Commented Jun 30, 2017 at 3:12

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.