Only show certain custom posts in all_items
When I register my custom post it adds a menu option to the WP Admin Dashboard that users can click and it lists all custom posts of that type. (Please see my image below if I am not making sense about the page I am describing).
That page listing all the custom posts of that type is good and working, but I only want that page to show certain custom posts. I only want to show custom posts whose post_meta my_flag
equals true. Everyone of my custom posts has a my_flag
post_meta set to true or false. I want my list page to only show custom posts whose my_flag
= true.
Is that possible to do when I register the custom post type? Even if it isn’t, how would I achieve this?
$labels = array(
'name' => _x( '_foo', 'Post Type General Name', '_foo' ),
'singular_name' => _x( 'Foo', 'Post Type Singular Name', '_foo' ),
'menu_name' => __( '_foo', '_foo' ),
'parent_item_colon' => __( 'Parent _foo', '_foo' ),
'all_items' => __( 'Foo Posts', '_foo' ),
'view_item' => __( 'View Foo', '_foo' ),
'add_new_item' => __( 'Add New Foo', '_foo' ),
'add_new' => __( 'Add New', '_foo' ),
'edit_item' => __( 'Edit Foo', '_foo' ),
'update_item' => __( 'Update Foo', '_foo' ),
'search_items' => __( 'Search Foo', '_foo' ),
'not_found' => __( 'Not Found', '_foo' ),
'not_found_in_trash' => __( 'Not found in Trash', '_foo' ),
);
$args = array(
'label' => __( 'foo', '_foo' ),
'description' => __( 'My custom posts', '_foo' ),
'labels' => $labels,
'supports' => array( 'title', 'revisions',
'taxonomies' => array( 'foo' ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => 'my-plugin-menu',
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'show_in_rest' => true,
'query_var' => true,
'menu_position' => 5,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'page'
);
register_post_type('foo', $args);
I set the post_meta like so:
add_post_meta($post_id, 'my_flag', true, true);
Leave an answer