Saurabh Gujarani
Saurabh Gujarani

Reputation: 511

Create Admin Menu for Post depending on category of post in Wordpress

I am trying to create a new admin menu with all post functionalities available So I am trying to create a new menu with having a post category 'news' and post-type - post So I have added below function but it still changing naming and functionality of original post menu.

function custom_post_type() 
{
    $labels = array(
        'name'                => _x( 'News', 'Post Type General Name', 'texdomain' ),
        'singular_name'       => _x( 'News', 'Post Type Singular Name', 'texdomain' ),
        'menu_name'           => __( 'News', 'texdomain' ),
        'parent_item_colon'   => __( 'Parent News', 'texdomain' ),
        'all_items'           => __( 'All News', 'texdomain' ),
        'view_item'           => __( 'View News', 'texdomain' ),
        'add_new_item'        => __( 'Add New News', 'texdomain' ),
        'add_new'             => __( 'Add New', 'texdomain' ),
        'edit_item'           => __( 'Edit News', 'texdomain' ),
        'update_item'         => __( 'Update News', 'texdomain' ),
        'search_items'        => __( 'Search News', 'texdomain' ),
        'not_found'           => __( 'Not Found', 'texdomain' ),
        'not_found_in_trash'  => __( 'Not found in Trash', 'texdomain' ),
    );

    // Set other options for Custom Post Type

    $args = array
    (
        'label'               => __( 'News', 'texdomain' ),
        'description'         => __( 'News news and reviews', 'texdomain' ),
        'labels'              => $labels,

        // Features this CPT supports in Post Editor

        'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', 'category'),

        // You can associate this CPT with a taxonomy or custom taxonomy. 

        'taxonomies'          => array( 'genres' ),
        /* A hierarchical CPT is like Pages and can have
        * Parent and child items. A non-hierarchical CPT
        * is like Posts.
        */  
        'hierarchical'        => false,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => false,
        'menu_position'       => 5,
        'can_export'          => true,
        'has_archive'         => true,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'capability_type'     => 'page',
    );

    // Registering your Custom Post Type
    register_post_type( 'Post', $args );

}

/* Hook into the 'init' action so that the function
* Containing our post type registration is not 
* unnecessarily executed. 
*/

add_action( 'init', 'custom_post_type', 0 );
function custom_post_news_search( $query ) {
    global $wp_query;

    if( is_admin() && $query->is_main_query() && isset( $_GET['post_type'] ) ) {

        //   $strSearchUrl = esc_attr($_GET['post_type']);
        $wp_query = new WP_Query(
        array(
            'post_type' => 'post',
            'category_name'    => 'News',
            )
        );
    }
} 

Upvotes: 3

Views: 2273

Answers (2)

Outsource WordPress
Outsource WordPress

Reputation: 3809

It's very easy to create a new sub-menu under Posts to list (or filter) a category under it. Just place the following code in your theme's 'functions.php'.

add_action('admin_menu', 'my_admin_menu'); 
function my_admin_menu() { 
    add_submenu_page('edit.php', 'News', 'News', 'manage_options', 'edit.php?category_name=news'); 
}

Upvotes: 4

Shahbaz A.
Shahbaz A.

Reputation: 4356

"Post" is already taken. So you cannot register another post type with this name. So with this out of the way you have three other options.

Option 1:

Create a custom post type naming "New Posts" or new_posts as slug and "News" as its taxonomy.

More information:

Option 2

If you insist on having post type as post. You can just create a new taxonomy named "News" and attach it to default posts.

More information:

Option 3

This one is slightly complex than others.

  • Create a New category named "News" for Posts.
  • Create an admin page.
  • Use WP_List_Table to get the same kind of view as default posts and custom post types.
  • Query posts having your "News" category and list them here.
  • (Optional) Create edit pages for your target posts in case you do not want to use the default one.

More information:

Hoping this will be useful. Happy Coding.

Upvotes: 4

Related Questions