0

I have a post type named "case". I want to create a "Sub-page" to list only my "cases". And add some custom HTML (search box, some labels, logos, etc..)

Anyone knows how can I do this?

Code to create the post_type.

function my_custom_post_type_case() {
    $labels = array(
        'name'               => _x( 'Cases', 'post type general name' ),
        'singular_name'      => _x( 'Case', 'post type singular name' ),
        'add_new'            => _x( 'Add New', 'case' ),
        'add_new_item'       => __( 'Add New Case' ),
        'edit_item'          => __( 'Edit Case' ),
        'new_item'           => __( 'New Case' ),
        'all_items'          => __( 'All Cases' ),
        'view_item'          => __( 'View Case' ),
        'search_items'       => __( 'Search Cases' ),
        'not_found'          => __( 'No cases found' ),
        'not_found_in_trash' => __( 'No cases found in the Trash' ), 
        'parent_item_colon'  => '',
        'menu_name'          => 'Cases'
    );
    $args = array(
        'labels'              => $labels,
        'description'         => 'Business Cases & Teaching Material',
        'public'              => true,
        'menu_icon'           => 'dashicons-book',
        'menu_position'       => 5,
        'map_meta_cap'        => true,
        'capability_type'     => array('case', 'cases'),
        'capabilities'        => array(
            'edit_post'             => 'edit_case',
            'edit_posts'            => 'edit_cases',
            'edit_others_posts'     => 'edit_other_cases',
            'publish_posts'         => 'publish_cases',
            'edit_publish_posts'    => 'edit_publish_cases',
            'read_post'             => 'read_cases',
            'read_private_posts'    => 'read_private_cases',
            'delete_post'           => 'delete_case'
        ),
        'supports'            => array( 'title', 'editor', 'wpcom-markdown', 'revisions' ),
        'has_archive'         => false
    );
    register_post_type( 'case', $args );    
}
add_action( 'init', 'my_custom_post_type_case' );

Here is the code to add the "Sub-page".

function register_adminMenu(){
    add_submenu_page('edit.php?post_type=case', 'My cases', 'My cases', 'manage_options', 'my_cases', 1);
};

function my_cases(){
    include_once(file_to_script);
}

The "include_once(file_to_script);" should be the script where list the "cases". I think that I have to extend the "WP_List_Table" class, but I'm not sure and I can't find this in Internet.

Thanks a lot

1 Answer 1

1

You can use the pre_get_posts filter to modify requested posts, this filter works in the admin area too, so you don't need to go to the trouble of creating a new post list table class

First lets add a query var to the URL of your 'My Cases' page:

function register_adminMenu(){
    add_submenu_page('edit.php?post_type=case&mycases=mine', 'My Cases', 'My Cases', 'manage_options', 'my_cases', 1);
}

Then, if that value is set, add the filter:

if ( !empty( $_GET['mycases'] ) ) {
    add_filter( 'pre_get_posts', function( $query ) {
        if ( is_main_query() && is_post_type_archive('case') && is_admin() ) {
             $query->set( 'author', $your_user_id );
        }
    });
}

Remember to set the user ID!

But wait, you want to show just your cases, aka cases with a specific author? If that's the case we don't even need the filter!

function register_adminMenu(){
    add_submenu_page('edit.php?post_type=case&author='. get_current_user_id(), 'My Cases', 'My Cases', 'manage_options', 'my_cases', 1);
}

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.