admin管理员组

文章数量:1129704

I want to add a new menu point to post that shows posts of a certain category. Adding a new page is easy if it is just a new post type. But I want to only show posts with a specific category and when updating posts make sure the category is checked.

IS there no way of doing this? I was hoping for some simple functon, like the way register_post_type() does it. As there doesn't seem to be, does anyone give me any tips about how to do this? Is it even possible? Or should I just use a custom post type?

I want to add a new menu point to post that shows posts of a certain category. Adding a new page is easy if it is just a new post type. But I want to only show posts with a specific category and when updating posts make sure the category is checked.

IS there no way of doing this? I was hoping for some simple functon, like the way register_post_type() does it. As there doesn't seem to be, does anyone give me any tips about how to do this? Is it even possible? Or should I just use a custom post type?

Share Improve this question edited Nov 27, 2023 at 8:41 Jesse Nickles 7357 silver badges19 bronze badges asked Jul 11, 2016 at 13:16 Arnold RimmerArnold Rimmer 1331 gold badge1 silver badge3 bronze badges 8
  • Yes, of course it is possible to list posts of a certain category! But to clarify your problem, can you elaborate what you mean by "I want to add a new menu point"? – TheDeadMedic Commented Jul 11, 2016 at 13:48
  • I want a page exactly like all posts, but one that only shows records from category x. I also want to use the update post filter to make sure that on this page items have category x selected.I know there is a cat query string var, but how can I add a new page as a subpage of posts that just lists all posts of category x??? Something like this maybe??? wp_create_list_table({cat: 'x'}); I can see how to add a subpage, but I have to create the content manually... but how??? What methods need to be called to do everything the all posts page does? – Arnold Rimmer Commented Jul 11, 2016 at 14:38
  • Just to clarify, this is for the backend (admin) not the front end. – Arnold Rimmer Commented Jul 11, 2016 at 14:51
  • I apologize if you answered this and I didn't catch the answer, or if I'm misunderstanding your issue. But why couldn't you just use the category filter from the all-posts listing? – Jeff Cohan Commented Jul 11, 2016 at 19:10
  • Because some clients want a separate page for the category, they have loads of posts. I suggested using that filter but they just want a link to a page that is all setup. – Arnold Rimmer Commented Jul 12, 2016 at 7:22
 |  Show 3 more comments

3 Answers 3

Reset to default 3

You can filter the posts list by appending ?category_name=xx to the admin posts list URL, and you can add a submenu page with that URL as the target via add_submenu_page:

add_action( 'admin_menu', 'wpd_admin_menu_item' );
function wpd_admin_menu_item(){
    add_submenu_page(
        'edit.php',
        'Page title',
        'Menu item title',
        'edit_posts', 
        'edit.php?category_name=somecat'
    );
}

You can loop posts by category name or ID:

$query = new WP_Query( array( 'category_name' => 'staff' ) );

or

$query = new WP_Query( array( 'cat' => 4 ) );

and just use good old fashion way to loop:

if ( $query->have_posts() ) :
    while ( $query->have_posts() ) : $query->the_post();
        get_title();
    endwhile;
else :
    echo 'No posts';
endif;

You need to create new custom template, get category ID, setup ID in new WP_Query and loop that.

Here is one documentation of how to use WP_Query()

The Category Posts Widget plugin will do this. https://wordpress.org/plugins/category-posts/

Category Posts Widget is a light widget designed to do one thing and do it well: display the most recent posts from a certain category.

Documentation for how to use it.

and

If you want to see how they did it, then this is their github repo .

本文标签: Display posts of only a certain category in WP Admin section