• Resolved Venutius

    (@venutius)


    Hi there, thanks for looking. I’ve probably got a bit of a newbie issue. I’ve created a custom post type which overall is working well, however I’ve noticed that it’s not showing in a get_post_types() call used by another plugin. I’m wondering if I’m missing something from my setup arguments. Here’s my code:

    function busl_register_post_type() {
    
    	$singular = __( 'Site' );
    	$plural = __( 'Sites' );
            //Used for the rewrite slug below.
            $plural_slug = str_replace( ' ', '_', $plural );
    
            //Setup all the labels to accurately reflect this post type.
    	$labels = array(
    		'name' 					=> $plural,
    		'singular_name' 		=> $singular,
    		'add_new' 				=> 'Add New',
    		'add_new_item' 			=> 'Add New ' . $singular,
    		'edit'		        	=> 'Edit',
    		'edit_item'	        	=> 'Edit ' . $singular,
    		'new_item'	        	=> 'New ' . $singular,
    		'view' 					=> 'View ' . $singular,
    		'view_item' 			=> 'View ' . $singular,
    		'search_term'   		=> 'Search ' . $plural,
    		'parent' 				=> 'Parent ' . $singular,
    		'not_found' 			=> 'No ' . $plural .' found',
    		'not_found_in_trash' 	=> 'No ' . $plural .' in Trash'
    	);
    
            //Define all the arguments for this post type.
    	$args = array(
    		'labels' 			  => $labels,
    		'public'              => true,
            'publicly_queryable'  => true,
            'exclude_from_search' => false,
            'show_in_nav_menus'   => true,
            'show_ui'             => true,
            'show_in_menu'        => true,
            'show_in_admin_bar'   => true,
            'menu_position'       => 6,
            'menu_icon'           => 'dashicons-admin-site',
            'can_export'          => true,
            'delete_with_user'    => false,
            'hierarchical'        => false,
            'has_archive'         => true,
            'query_var'           => true,
            'capability_type'     => 'post',
            'map_meta_cap'        => true,
            // 'capabilities' => array(),
            'rewrite'             => array( 
            	'slug' => strtolower( $plural_slug ),
            	'with_front' => true,
            	'pages' => true,
            	'feeds' => false,
    
            ),
            'supports'            => array( 
            	'title',
    			'thumbnail'
            )
    	);
    
            //Create the post type using the above two variables.
    	register_post_type( 'buslsite', $args);
    }
    add_action( 'init', 'busl_register_post_type' );
    
    
Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter Venutius

    (@venutius)

    The line that’s not returning my post type is as follows:

    $posttypes = get_post_types( array('public' => true ), 'names' );

    Thread Starter Venutius

    (@venutius)

    I’ve done some work on this and it seems to be a timing issue, the plugin calling get_post_types seems to be calling that function before my post type is registered.

    Moderator bcworkz

    (@bcworkz)

    I think you’re right about timing. Using the “init” action is the proper hook for adding post types. What plugin is using get_post_types()? In what context is the plugin calling get_post_types()? If it is called in plugin code without a callback, that’s much too early. If it is also hooked to “init”, add your callback with a smaller priority number to be sure yours is called before the others.
    add_action( 'init', 'busl_register_post_type', 1 );

    Thread Starter Venutius

    (@venutius)

    Yes that worked, thanks!

    it’s LH Logged in post status.

    Thanks again for your help.

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Custom post type not showing in get_post_types array’ is closed to new replies.