Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8920231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:13:00+00:00 2026-06-15T06:13:00+00:00

I have found out a way to do this is in WordPress: Say you

  • 0

I have found out a way to do this is in WordPress:

Say you post something under a sub-category called “news-sub”, which parents category is “news”. By deafult, the wordpress post will get posted under both news and news sub even if you only select it to be posted under the category news-sub. I’ve found a way for a post to only get posted in its sub category when you select it to be posted in just its sub category. Here is the code that inserts in functions.php:

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()){
    if(is_category()){
      add_action('posts_where', 'current_category_only');
    }
  }
}

function current_category_only($where) {
  $current_cat = get_query_var('cat');
  if($current_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;
}

This code works awesome. But, it doesn’t work with plugins. For example… I have a plugin that posts thumbnails and titles for your selected category. Say I select it to display only the “news” category.. if I posted something just under just the “news-sub” category, it still gets displayed via the plugin, regardless of my function code I mentioned earlier. I tried copy and pasting the function code into the plugin, but it didn’t work. 🙁

So, how would I get this function to work with plugins that display categories? Would I modify it and leave it in the functions.php file, would I modify it and copy and paste it into my plugin file or how would I modify it at all? Any help would be tremendously appreciated! 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T06:13:01+00:00Added an answer on June 15, 2026 at 6:13 am

    The behavior you are describing is absolutely expected. Categories are hierarchical, so a parent category is expected to contain the subcategories and their posts.

    Your posts_where filter is trying to change the behavior to be more like tags. But some functions like get_posts() do not run these filters:

    Certain functions which retrieve posts do not run filters, so the posts_where filter functions you attach will not modify the query. To overcome this, set suppress_filters to false in the argument array passed to the function. The following code sample illustrates this.

    $posts = get_posts( array( 'suppress_filters' => FALSE ) );
    

    It depends on your plugin code if the filter is observed or not, so there is no clean way to rewrite the category logic. You might be better off using tags for the non-hierarchical semantics you seem to need. Or have a look at custom taxonomies which

    are an extremely powerful way to group various items in all sorts of ways.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have recently found out that there exists a method called nth_element in the
I have found plenty of information out there about this error: 'ERROR: Could not
I have got a proj work, which works in this way... An app which
As I have found out there is no way to change the device brightness
I have found out that I am doing WPF wrong and frustratingly must overhaul
I have to start work on phone gap. Till now i have found out
I have recently found out that no argument constructor and multiple argument constructor cannnot
I'm doing some fairly simple cross-platform TCP socket programming. I have unfortunately found out
I found out that in order to open a Gmail compose screen you'd have
I have found many similar posts and even tried to find out how to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.