0

I'm having a bit of trouble (i think) over custom post types in Wordpress.

I have created multiple post types and they all work, but there is this one thing which is bothering me.

Lets say I have 3 custom post types. Movies, Albums and Video Games, each one of them has a set number of posts.

Now, in the sidebar menu i admin, if I click on Movies' "All Movies" it shows the most recent entries, regardless if its movies, albums or video games..

Is there a way to to separate them, so when I press "All Movies", it only shows posts in post type Movies?

I might add that I have used smashing's guide to creating custom post types.

Here is an example of the code I used to register a post type:

function post_type_movies() {

    $labels = array(
        'name'               => _x( 'Movies', 'post type general name' ),
        'singular_name'      => _x( 'Movie', 'post type singular name' ),
        'add_new'            => _x( 'Add New', 'Movie' ),
        'add_new_item'       => __( 'Add New Movie' ),
        'edit_item'          => __( 'Edit Movie' ),
        'new_item'           => __( 'New Movie' ),
        'all_items'          => __( 'All Movies' ),
        'view_item'          => __( 'View Movie' ),
        'search_items'       => __( 'Search Movies' ),
        'not_found'          => __( 'No Movies found' ),
        'not_found_in_trash' => __( 'No Movies found in the Trash' ), 
        'parent_item_colon'  => '',
        'menu_name'          => 'Movies Post',

    );

    $args = array(
        'labels'             => $labels,
        'description'        => '',
        'public'             => true,
        'menu_position'      => 30,
        'supports'           => array('title','editor','thumbnail', 'custom-fields'),
        'has_archive'        => true,
        'menu_icon'          => 'dashicons-portfolio',
        'post_tags'          => true,
    );
    register_post_type('movie-post', $args);

}
add_action('init','post_type_movies');

Here are other post types for comparison: // Post Type - Games

function post_type_games() {

    $labels = array(
        'name'               => _x( 'Games', 'post type general name' ),
        'singular_name'      => _x( 'Game', 'post type singular name' ),
        'add_new'            => _x( 'Add New', 'game' ),
        'add_new_item'       => __( 'Add New Game' ),
        'edit_item'          => __( 'Edit Game' ),
        'new_item'           => __( 'New Game' ),
        'all_items'          => __( 'All Games' ),
        'view_item'          => __( 'View Game' ),
        'search_items'       => __( 'Search Games' ),
        'not_found'          => __( 'No Games found' ),
        'not_found_in_trash' => __( 'No Games found in the Trash' ), 
        'parent_item_colon'  => '',
        'menu_name'          => 'Games',

    );

    $args = array(
        'labels'             => $labels,
        'description'        => '',
        'public'             => true,
        'menu_position'      => 40,
        'supports'           => array('title','editor','thumbnail', 'custom-fields'),
        'has_archive'        => true,
        'menu_icon'          => 'dashicons-welcome-widgets-menus',
        'post_tags'          => true,
    );
    register_post_type('game', $args);

}
add_action('init','post_type_games');

// Post Type - Locations

function post_type_locations() {

    $labels = array(
        'name'               => _x( 'Locations', 'post type general name' ),
        'singular_name'      => _x( 'Location', 'post type singular name' ),
        'add_new'            => _x( 'Add New', 'book' ),
        'add_new_item'       => __( 'Add New Location' ),
        'edit_item'          => __( 'Edit Location' ),
        'new_item'           => __( 'New Product' ),
        'all_items'          => __( 'All Locations' ),
        'view_item'          => __( 'View Location' ),
        'search_items'       => __( 'Search Locations' ),
        'not_found'          => __( 'No Locations found' ),
        'not_found_in_trash' => __( 'No Locations found in the Trash' ), 
        'parent_item_colon'  => '',
        'menu_name'          => 'Locations',

    );

    $args = array(
        'labels'             => $labels,
        'description'        => '',
        'public'             => true,
        'menu_position'      => 30,
        'supports'           => array('title','editor','thumbnail', 'custom-fields'),
        'has_archive'        => true,
        'menu_icon'          => 'dashicons-location',
        'post_tags'          => true,
    );
    register_post_type('location', $args);

}
add_action('init','post_type_locations');
  • 2
    Can you share a link with the code you used to register the custom post types? Community would do a better work helping by providing that. – marcovega Jul 2 '15 at 11:16
  • Sure, I used this guide by smashing magazine – TheTimeRanger Jul 2 '15 at 11:29
  • What you want, if I understand you, is how it is supposed to work. Something is wrong with your code or something else-- plugin, theme-- is causing trouble. What do the other CPT registrations look like? – s_ha_dum Jul 2 '15 at 14:09
  • @TheTimeRanger so from my understanding of your question, no matter what post type you're on it always shows Movie Posts? Have you tried disabling all plugins and switching to the default theme? – Howdy_McGee Jul 2 '15 at 14:11
  • The important part is register_post_type('movie-post', $args); where there different names for the post type are necessary. If those are different I'd suggest disabling all plugins to see if the issue persists. – kraftner Jul 2 '15 at 15:08
0

The issue is that pre_get_posts runs on both the Front End and on the Admin Side unless you specifically tell it not to, here's the function you supplied with a small twist:

function pregp_archive_ppp_wpse_108225( $qry ) {
    if( is_admin() ) {
        return;
    }

    if( $qry->is_main_query() && $qry->is_archive() ) {
        $qry->set( 'posts_per_page', 5 );
        $qry->set( 'post_type', array( 'post', 'location', 'game' ) );
    }
}
add_action( 'pre_get_posts', 'pregp_archive_ppp_wpse_108225' );

Note the first conditional is_admin() - this will test against any / every admin page so that the following changes will only apply to front-end queries.

  • 1
    Thank you so very very much @Howdy_McGee. You solved two issues I had in one go! Thank you, i'm quite new to Wordpress development, so I still have many things I have yet to learn! – TheTimeRanger Jul 2 '15 at 18:44

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.