0

I would like to exclude the current category on my WordPress post and I'm a bit stuck with this, would you help me?

Here something to grab all the categories from this categorie ID (My category is named "Villes").

$args = array( 'parent' => 779 );
$categories = get_categories( $args );

And here something to loop throug and display my super results

foreach ( $categories as $category ) {
    $value[] = array(
        'field_5cd186302ef1c' => $category->name,
    );
};
return $value;    

As you can see I just insert them into an array. But my problem is that I want to exclude the current category from this. For example let's assume that I have 3 subcategories in my main category.

Villes(category)

  • London
  • Paris
  • Amsterdam

And i just want to show this list, but remove one or more of them (for example Paris) because it belongs to my current post.

To sum this up, how do I remove the current subcategory from the list?


So for the categories they are created and assigned to a post with WP All Import Pro, I've created another foreach loop to assign every post created to a special category. In fact I've created a form for my posts with ACF, and inside of it I've created two repeater called "taxonomy". I have this first repeater wich retrieve all the current subcategory from a major category called "villes". This code may be clearer:

$categories = get_the_terms( $post_id, 'category' );

With the code above, I grab all the current category for the actual post

foreach ($categories as $category ) {
    if ( $category->parent == 779 ) {    
        $value[] = array(
            'field_5cc6f8b858135' => $category->name,
        );
    };
}
return $value;

So a foreach loop to create all the terms retrieve by $categories, then if "parent" of those category are equal to the parent_id that i want (in my example this id is equal to 779), write all of them to the current repeater field (here field called: field_5cc6f8b858135 )

This first one is working really well, no prob with it, it retrieve me the good subcategories.

But I want a second repeater with all the subcategories (except the current one) that are belong to "villes" (Id: 779). That's why I'm looking for creating a loop that currently show all the subcategory from "villes" (ID: 779), except the one that are affected to the actual post.

So, as I said I tried:

$args = array( 'parent' => 779 );
$categories = get_categories( $args );

To define what kind of subcategory I want, here all the childrens of "villes" (ID: 779). Then I tried a foreach loop to grind through and insert them into my field.

foreach ( $categories as $category ) {
    $value[] = array(
        'field_5cd186302ef1c' => $category->name,
    );
};
return $value;    

But my question I still have, how do I complete (or rewrite) this code in order to exclude all the subcategory that belong to the actual post? My goal is still to show all of the subcategory from "villes", but not the current one.

  • 1
    You would modify the posts retrieved using the pre_get_posts filter, creating new queries to change what gets shown on a page can make you site 2x slower, if not more. How are you determining the current category? And what do you want to happen if a post is in several categories? – Tom J Nowell May 9 at 15:39
2

First you need to get the categories of the current post, and then you can 'exclude' them by sending them through via the get_categories arguments array as $args['exclude'].

get_categories() uses get_terms(), you can see the list of available arguments you can use here.

We get the post terms with wp_get_post_terms() and make sure that we only get an array of ids and not an array of objects by passing it array("fields" => "ids") );

$args = array( 'parent' => 779 );

//Get the current post categories
$post_categories = wp_get_post_terms( get_the_ID(), 'category', array("fields" => "ids") );
if ( ! empty( $post_categories ) ) {
  //Here we make use of the exclude argument
  $args['exclude'] = $post_categories;
}

$categories = get_categories( $args );

Then you can just loop through your categories with your second snippet as you were earlier.

foreach ( $categories as $category ) {
    $value[] = array(
        'field_5cd186302ef1c' => $category->name,
    );
};
return $value; 

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.