Custom post type auto assign specific category

[ad_1]

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'
      ));
     $taxquery = array(
        array(
            'taxonomy' => 'category/Taxomony_SLUG',
            'field' => 'term_id',
            'terms' => PUT_YOUR_TERM_ID, // array(12, 23)
            'operator'=> 'IN'
        )
    );
    $query->set( 'tax_query', $taxquery );
   }
}
add_action( 'pre_get_posts', 'special_add_custom_types' );

Hi @ctrlsync

Try above code, hope this code will help you to show the result with the preselected category.

Note: Make sure if you have custom taxonomy use tax_query if you are using default category, then use $query->set( 'cat',$cat_id); to set the ID of category/term ID.

Hope this will work.

Thread Starter
Hisham

(@ctrlsync)

Thanks for your response, I am having trouble making this work, here is what I did:

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'
      ));
     $taxquery = array(
        array(
            'taxonomy' => 'category/daily-special',
            'field' => 'term_id',
            'terms' => 'daily-special', // array(12, 23)
            'operator'=> 'IN'
        )
    );
    $query->set( 'tax_query', $taxquery );
   }
}
add_action( 'pre_get_posts', 'special_add_custom_types' );

Is ‘taxonomy’ => ‘category/daily-special’, not supposed to be category/then the category I want, and ‘terms’ => ‘daily-special’ are the two items I set to use the category daily-special.

Thanks

You have to make changes in two places:
In taxonomy, you have to make changes, your custom taxonomy name and in terms you are using category slug so in field you have to write a slug like below:

'taxonomy' => 'daily-special',
'field' => 'slug',

Here is full code:

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'
      ));
     $taxquery = array(
        array(
            'taxonomy' => 'daily-special',
            'field' => 'slug',
            'terms' => 'daily-special', // array(12, 23)
            'operator'=> 'IN'
        )
    );
    $query->set( 'tax_query', $taxquery );
   }
}
add_action( 'pre_get_posts', 'special_add_custom_types' )

For more information about the tax query, look at this documentation: https://developer.projectdmc.org/reference/classes/wp_query/#taxonomy-parameters

If you are using default category for Specials post type then you have to use the category parameters, here you can see: https://developer.projectdmc.org/reference/classes/wp_query/#category-parameters

So make sure before changing the query.

Thread Starter
Hisham

(@ctrlsync)

Pardon my ignorance but this still does not work.

When I replace this code :

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' );

Wit the code you posted above:

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'
      ));
     $taxquery = array(
        array(
            'taxonomy' => 'daily-special',
            'field' => 'slug',
            'terms' => 'daily-special', // array(12, 23)
            'operator'=> 'IN'
        )
    );
    $query->set( 'tax_query', $taxquery );
   }
}
add_action( 'pre_get_posts', 'special_add_custom_types' )

It no longer registers my old Special post on the front page and any new special posts I make do not end up in the daily-special category?

I must be misunderstanding what to do ?

Thanks.

Looks like you are using default WordPress category as taxonomy for your custom post type Specials, as per your first code in question, so you have to set category just below query:

$query->set( 'category_name',  'daily-special' );
$query->set( 'post_type', array(
       'post', 'specials'
      ));

Hope you understand.

Thread Starter
Hisham

(@ctrlsync)

Thanks for all your help, I was able to finally get this to work

function auto_select_daily_specials_category( $post_ID ) { 
	$daily_special_cat = get_term_by('slug', 'daily-special', 'category'); 
	if ( !has_term( 'daily-special', 'category', $post_ID ) ) { 
		wp_set_post_categories( $post_ID, array( $daily_special_cat->term_id ), true ); 
	} 
} add_action( 'save_post', 'auto_select_daily_specials_category' );

This code defaults to daily-post when you open a new post. This works perfectly.

Thanks for all your help I will mark as solved!

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer