Custom post type auto assign specific category
-
Hello all, I have searched around and found answers but none seem to work, so maybe its an issue with my existing code. I created a custom post type for a restaurant website called “Specials.” It works fine and updates the home page with the blog section when a new special is added however you have to select the category “daily-special” which I would like to have either pre-selected or automatically placed into this category upon posting a new Special.
Here is the code I am using in my divi child theme to make the custom post type work;
// custom post type function function create_posttype() { register_post_type( 'Specials', // CPT Options array( 'labels' => array( 'name' => __( 'Specials' ), 'singular_name' => __( 'Special' ) ), 'public' => true, 'has_archive' => true, 'rewrite' => array('slug' => 'daily-specials'), 'show_in_rest' => true, 'taxonomies' => array( 'category' ), ) ); } // Hooking up our function to theme setup add_action( 'init', 'create_posttype' ); function special_add_custom_types( $query ) { if( (is_category() || is_tag()) && $query->is_archive() && empty( $query->query_vars['suppress_filters'] ) ) { $query->set( 'post_type', array( 'post', 'specials' )); } } add_action( 'pre_get_posts', 'special_add_custom_types' );
Any help is much appreciated, Thanks.
Viewing 8 replies - 1 through 8 (of 8 total)
Viewing 8 replies - 1 through 8 (of 8 total)
- The topic ‘Custom post type auto assign specific category’ is closed to new replies.