0

I have created a custom post type 'courses' but it is not showing up when I do a var_dump of get_post_types(). here is my code.

add_action( 'init', 'create_post_types' );
function create_post_types() {
    $courseargs = array(
            'labels' => array(
                        'name' => 'Courses',
                        'singular_name' => 'Course',
                       ),
            'public'             => true,
            'publicly_queryable' => true,
            'show_ui'            => true,
            'show_in_menu'       => true,
            'query_var'          => true,
            'rewrite'            => array( 'slug' => 'courses' ),
            'capability_type'    => 'post',
            'taxonomies'         => array( 'category' ),
            'has_archive'        => true,
            'hierarchical'       => false,
            'menu_position'      => null,
            'menu_icon'          => 'dashicons-welcome-learn-more',
            'supports'           => array( 'title', 'editor', 'thumbnail' )
    );
register_post_type( 'courses', $courseargs );
}

// add the featured image thumbnail to the admin courses column
add_filter( 'manage_courses_posts_columns', 'add_img_column' );
add_action( 'manage_courses_posts_custom_column' , 'manage_img_column', 10, 2 );

// size the thumbnail
add_image_size( 'admin-list-thumb', 80, 80, false );

function add_img_column($columns) {
    $columns = array(
        'cb' => '<input type="checkbox" />',
        'title' => 'Title',
        'author' => 'Author',
        'categories' => 'Categories',
        'tags' => 'Tags',
        'comments' => '<span class="vers"><div title="Comments" class="comment-grey-bubble"></div></span>',
        'featured_thumb' => 'Thumbnail',
        'date' => 'Date'
    );
    return $columns;
}

function manage_img_column( $column, $post_id ) {
    switch ( $column ) {
    case 'featured_thumb':
        echo '<a href="' . get_edit_post_link() . '">';
        echo the_post_thumbnail( 'admin-list-thumb' );
        echo '</a>';
        break;
    }
}

// Show posts of 'post', 'page' and 'courses' post types on home page
function add_my_post_types_to_query( $query ) {
  if ( is_home() && $query->is_main_query() )
    $query->set( 'post_type', array( 'post', 'page', 'courses' ) );
  return $query;
}
add_action( 'pre_get_posts', 'add_my_post_types_to_query' );

var_dump(get_post_types());

This is what the var_dump gives me.

array(8) {
  ["post"]=>
  string(4) "post"
  ["page"]=>
  string(4) "page"
  ["attachment"]=>
  string(10) "attachment"
  ["revision"]=>
  string(8) "revision"
  ["nav_menu_item"]=>
  string(13) "nav_menu_item"
  ["custom_css"]=>
  string(10) "custom_css"
  ["customize_changeset"]=>
  string(19) "customize_changeset"
  ["oembed_cache"]=>
  string(12) "oembed_cache"
}

Any help would be greatly appreciated.

1

Regarding the var_dump, "courses" isn't showing up because the var_dump is running outside of any action. So it gets triggered before the "init" action runs, which is where the post type is registered.

Regarding the main loop, I pasted your code into the default Twenty Seventeen theme's functions.php file, and added a course via the dashboard, and it showed up on the homepage for me.

  • Is there a way I can test to see if it is showing up other than using var_dump? – Jason May 17 '18 at 15:04
  • If you put the var_dump inside your add_my_post_types_to_query function, that should work. – SeventhSteel May 17 '18 at 15:18

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.