How To Disable Displaying Articles Of A Subcategory In The Main Category of WordPress

WordPress uses two primary taxonomies for website organization: Posts and Pages. Posts are further categorized into categories and sometimes into subcategories. For instance, you might have a category like ‘Cars’ with associated subcategories like ‘Diesel,’ ‘Gasoline,’ and ‘Electric.’ The issue arises when an article is assigned solely to a subcategory. It not only appears in that subcategory (‘Electric’) but also in the main category (‘Cars’). These categories are commonly referred to as parent and child. Essentially, if you assign an article to the ‘Electric’ subcategory, it will simultaneously display in the main ‘Cars’ category. As a result, this article will be in two places. This isn’t always desired. In this article, we’ll explore how to disable displaying articles of a subcategory in the main category, using a snippet.

How To Disable Displaying Articles Of A Subcategory In The Main Category

In the diagram, you can see that if you create an article and assign it the ‘Electric’ category, it will display not only there but also in the main ‘Cars’ category. Is it possible to change this behavior so that the article appears only in the category to which it’s actually assigned? Yes, it is possible, and it’s not even significantly complex.

To achieve this without installing additional plugins, we’ll use a code snippet with the popular WPCode plugin, which should be installed by anyone looking to modify the properties and behavior of their WordPress site. A snippet is simply a short piece of code in PHP or another language that modifies the properties and behavior of the website.

Installing the WPCode plugin

To insert codes into your website, you have two options. You can hardcode the code into the appropriate file or use a plugin that dynamically inserts portions of code into your website during runtime. The latter option is better for a simple reason: it’s more secure, and your code won’t get erased with each update that overwrites the file containing your code.

Here’s how to install the WPCode plugin:

  • Click on ‘Plugins‘ in the left-hand menu.
  • Select ‘Add New‘ from the submenu.
  • Notice the search field in the top right corner.
  • Type in the keyword ‘WPCode.
  • Once WordPress finds the plugin, install and activate it.
  • If you’re interested in a complete article where I describe working with this plugin, take a look here: How to Insert Custom Code in WordPress.
Jak do WordPressu vkládat vlastní kódy

How to Disable Displaying Subcategory Articles in the Main WordPress Category Using a Snippet

If you want to disable the display of subcategory articles in the main category, use the WPCode plugin mentioned earlier. Through this plugin, insert the following PHP code into your website:

				
					function wpse_filter_child_cats( $query ) {

if ( $query->is_category ) {
    $queried_object = get_queried_object();
    $child_cats = (array) get_term_children( $queried_object->term_id, 'category' );

    if ( ! $query->is_admin )
        //exclude the posts in child categories
        $query->set( 'category__not_in', array_merge( $child_cats ) );
    }

    return $query;
}
add_filter( 'pre_get_posts', 'wpse_filter_child_cats' );
				
			

Conclusion

Once you insert this short code into your website and save and activate the snippet, articles will begin to display only in the categories to which they are assigned. They won’t appear in other main categories anymore.

The website is created with care for the included information. I strive to provide high-quality and useful content that helps or inspires others. If you are satisfied with my work and would like to support me, you can do so through simple options.

Byl pro Vás tento článek užitečný?

Klikni na počet hvězd pro hlasování.

Průměrné hodnocení. 0 / 5. Počet hlasování: 0

Zatím nehodnoceno! Buďte první

Jak užitečný vidíte tento článek.

Sledujte mě na sociálních médiích.

Je mi líto, že pro Vás nebyl článek užitečný.

Jak mohu vylepšit článek?

Řekněte mi, jak jej mohu zlepšit.

newsletter

Subscribe to the Newsletter

Stay informed! Join our newsletter subscription and be the first to receive the latest information directly to your email inbox. Follow updates, exclusive events, and inspiring content, all delivered straight to your email.

Odebírat
Upozornit na
guest
0 Komentáře/ů
Vložené zpětné vazby.
Zobrazit všechny komentáře.

Pokud mi chcete napsat rychlou zprávu, využije, prosím, níže uvedený
kontaktní formulář. Děkuji.

Další Kontaktní údaje