2

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?

  • 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 Jul 11 '16 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 Jul 11 '16 at 14:38
  • Just to clarify, this is for the backend (admin) not the front end. – Arnold Rimmer Jul 11 '16 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 Jul 11 '16 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 Jul 12 '16 at 7:22
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'
    );
}
0

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()

  • Thanks, I know about the query. I'm looking to not do everything manually, there must be some API method that draws the "all posts" or "pages" pages, that I can just provide some kind of argument to to only show post that are in category "x"? – Arnold Rimmer Jul 11 '16 at 14:43
  • Realy don't know any plugin for that. I think exists but you need to look. Some templates support that but my suggestion for you is to just made new custm template, copy 1:1 some original code from your template and just adjust query code. – Ivijan Stefan Stipić Jul 11 '16 at 14:55
0

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 .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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