0
votes

I have added this function in my functions.php file to show category option for wordpress pages. It works fine.

function support_category_for_pages() {  
    // Add category support to pages
    register_taxonomy_for_object_type('category', 'page');  
}
add_action( 'init', 'support_category_for_pages' );

But is it possible to show/limit some categories for pages only(they must not appear under posts) ?

I hope I am writing this correctly. Basically the requirement is to keep different categories for post & pages and they should not appear in each other's category option.

1

1 Answers

0
votes

There are two approaches i can think off. I am not sure how you want it to work regarding the archive page and search page so second approach may not work for you.

First solution creating templates: For example category-news.php

Inside you can modify the query specificly for this category

$args = array(
'post_type' => 'posts'
//add more arguments if needed
);

$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {
    while ( $the_query->have_posts() ) {
      $the_query->the_post();
      // Do Stuff
    } // end while
} // endif

// Reset Post Data
wp_reset_postdata();

Second solution using pre_get_posts:

 function wp50_exclude_post_type_pages() {
 //in the array you can add ids, slugs or names
 if ( is_category( array( 9, 'cat2', 'Category 3' )) && $query->is_main_query() ) {
   //here we tell the query to show posts type only 
   $query->set( 'post_type', 'posts' );
   return $query;
 }
 
}
add_action( 'pre_get_posts', 'wp50_exclude_post_type_pages');