Support » Fixing WordPress » Publishing a Post Only in Its Sub-Category?

  • I have a category titled “news” then a test sub category called “news-sub”. By default, if you publish a post under news-sub it’s displayed under both the news category and the news-sub category. That’s alright. But sometimes, I would like the post to be published under only the sub category and not the parent sub category. I thought it would be as easy as just checking the category “news-sub” and not “news”. But the post still gets published under both the sub category and parent category when you publish it just under the sub category. 🙁

    So, is there a way to publish a post so it is displayed only in its sub category and not both the parent category and sub category? Any help would be tremendously appreciated! 😀

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator keesiemeijer


    Try it with this in your theme’s functions.

    add_action( 'pre_get_posts', 'my_post_queries' );
    function my_post_queries( $query ) {
      // not an admin page and is the main query
      if (!is_admin() && $query->is_main_query()){
          add_action('posts_where', 'current_category_only');
    function current_category_only($where) {
      $current_cat = get_query_var('cat');
        global $wpdb;
        return " AND ( $wpdb->term_relationships.term_taxonomy_id IN ($current_cat) ) AND $wpdb->posts.post_type = 'post' AND ($wpdb->posts.post_status = 'publish' OR $wpdb->posts.post_status = 'private')";
      return $where;

    I added that to my functions page and when I submit a post only under “news-sub” it still gets displayed under “news” as well. I even tried refreshing my permalinks. Perhaps I have to delete something from my current functions.php file to in order for your code to work??

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Publishing a Post Only in Its Sub-Category?’ is closed to new replies.