0

I have a category named "SERVICES" and I have added three subcategories for "SERVICES" category. I have added three posts for the category "SERVICES" and several posts for the subcategories. Some of my posts don't belong to the category "SERVICES" but only belong to the subcategories of "SERVICES" but when I am choosing "SERVICES" category from the dropdown in the post list page and trying to filter the posts, I am getting all the posts from the subcategories also. I just need the posts from the category itself not its subcategories. Is there any way to do the same.

enter image description here

  • Possible duplicate of Exclude sub category posts from category display – cybmeta Dec 15 '15 at 8:08
  • Hello @cybmeta It isn't the duplicate of the post that you are showing me. I know how to deal category subcategory in the frontend. what I have asked here is how to do it in the backend where we filter the post according to the category. Its for making the backend user friendly for my client. I have edited my question and added where I want that thing to work. – Designer Coder Dec 15 '15 at 8:35
  • Just click on any category in the list table, and you got posts only of that category. – Mayeenul Islam Dec 15 '15 at 8:43
  • Hi @Mayeenul. Yes I am getting that but along with it I am also getting the posts from the subcategories which I want to avoid. I have some posts that are just under the subcategories and they are getting shown. How to just get posts from the category itself and not its subcategories ???? Please Help:( – Designer Coder Dec 15 '15 at 8:45
  • How to just get posts from the category itself and not its subcategories ???? :( - If so, I think @cybmeta is right then. :) – Mayeenul Islam Dec 15 '15 at 8:47
0

It's all about the pre_get_post hook where you can affect the Wp_Query instance. Here is a quick approach:

<?php # -*- coding: utf-8 -*-

/**
 * Plugin Name: Post list table discrete category filter
 */

namespace Wpse211851;

use
    WP_Query;

add_action( 'load-edit.php', __NAMESPACE__ . '\register' );

/**
 * @wp-hook load-edit.php
 *
 * register the hook on pre_get_post
 */
function register() {

    if ( ! isset( $_GET[ 'cat' ] ) || 0 === (int) $_GET[ 'cat' ] )
        return;

    add_filter( 'pre_get_posts', __NAMESPACE__ . '\discrete_category_tax_query' );
}

/**
 * Change the tax query according to the request
 *
 * @param WP_Query $query
 */
function discrete_category_tax_query( \WP_Query $query ) {

    if ( ! $query->is_main_query() )
        return;

    $query_args = array(
        'taxonomy'         => 'category',
        'field'            => 'term_id',
        'terms'            => [ (int) $_GET[ 'cat' ] ],
        'include_children' => FALSE
    );

    /**
     * append a tax query to an existing WP_Query object
     * @link http://wordpress.stackexchange.com/a/98143/31323
     */
    $query->tax_query->queries[] = $query_args;
    $query->query_vars[ 'tax_query' ] = $query->tax_query->queries;
}

The function discrete_category_tax_query() updates the main Wp_Query instance in a way that the tax query is replaced by completely new parameter. These will query for the exact category excluding any child terms.

However, this will break any other tax query that might be requested and is therefore just a example on how to change the query on admin pages.

A more solid solution should merge the new tax query parameter with any possibly existing ones!

  • Thank You Sir @david. It has started to answer my question but I am getting the following errors.` Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'contact_form' not found or invalid function name in C:\xampp\htdocs\b.a-wordpress\wp-includes\plugin.php on line 503 Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'register_my_menus' not found or invalid function name in C:\xampp\htdocs\b.a-wordpress\wp-includes\plugin.php on line 503` – Designer Coder Dec 15 '15 at 10:36
  • Can you please point me what I must do next to remove this errors? A link to some resource will be highly helpful. Thanks for your precious time @David – Designer Coder Dec 15 '15 at 10:46
  • In fact, it answers your question. Those error messages are a sign of that you're running some other plugins or a theme which is not the default one. One or more of these seems to cause the errors. Deactivate them, to find out who. – David Dec 15 '15 at 13:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.