1

I've been trying to display recent posts for months excluding the current category. I have already researched on several forums and almost everything on StackOverflow and unfortunately I did not achieve success on my project.

I need help with the following functions:

My recent posts work normally. I can delete the current post and display all others.

What I would like to do is display all the latest posts excluding the current category.

Examples:

My categories: example1 / example2 / example3 /

Let's say the user is viewing this link:

Link 1 =

domain.com/category/example1/examplepost

In recent posts I would like to display the last 5 posts from the category "example2" and "example3".

Link 2 =

domain.com/category/example2/examplepost

In recent posts I would like to display the last 5 posts from the category "example1" and "example3".

I have the following code that I use in function.php

add_action('pre_get_posts', 'wpa_12345' );
function wpa_12345( $wp_query ) {
 $excluded = array(5);
 if( !is_admin() ) {
 $wp_query->set('category__not_in', $excluded);
 }
}
add_filter('pre_get_posts', 'exclude_category');

This code allows me to delete a category that I choose.

I would like some code that would automatically identify the current category to exclude from recent posts.

I followed Abhik user's guidelines ... See if the code was assembled correctly in functions.php

function wpa_12345( $wp_query ) {

 //Get the current category ID
 $catID = get_queried_object_id();

 //Pass it to the Query arguments
 $args = array(
 //Your other arguments here
 'category__not_in' => array($catID),
 );

 $excluded_posts = new WP_Query($args);

 if( !is_admin() ) {
 $wp_query->set('category__not_in', $excluded);
 }
}
add_filter('pre_get_posts', 'exclude_category');

Even reassembling the code, I didn't get what I expected.

1 Answer 1

0

Assuming you are using custom query to fetch the latest posts from other categories. In that case, you don't need to hook into pre_get_posts as the category__not_in parameter can be passed directly into the query arguments.

//Get the current category ID
$catID = get_queried_object_id();

//Pass it to the Query arguments
$args = array(
    //Your other arguments here
    'category__not_in' => array($catID),
);
$excluded_posts = new WP_Query($args);

EDIT

function wpse_exclude_current_cat( $query ) {
    if ( !is_admin() && !$query->is_main_query() ) {
        if ( is_category() ) {
            $catID = get_queried_object_id();
            $query->set( 'category__not_in', array($catID) );
        }
    }
}
add_action( 'pre_get_posts', 'wpse_exclude_current_cat' );
5
  • Thank you very much for your reply. I updated my question by inserting the change you indicated. Please see if I did the correct procedure.
    – Anonymous
    May 8, 2021 at 13:57
  • No, you don't. Update your question with the codes that displays the related posts in category page.
    – Abhik
    May 8, 2021 at 17:22
  • I use the default recent posts system of the installed theme. My theme pulls the standard recent WordPress posts. This theme does not personalize calls from recent posts. Your code may work and I don't know how to build it. Could you update your code to be used in functions.php? Or where else can I use your code?
    – Anonymous
    May 8, 2021 at 17:42
  • @Anonymous I have added another bit of code. That should work in theory, You might need to edit it to fit in your need.
    – Abhik
    May 8, 2021 at 17:48
  • Thanks for the feedback. But unfortunately this second code that you sent, didn't work either. What confuses me is knowing that the first code I inserted in the question works to exclude a specified category. My example was the category ID 5. That way I can delete the category with ID 5. But I can't make the category be automatically identified to exclude it from recent posts. I really need this function even though I have to change the WordPress core files.
    – Anonymous
    May 8, 2021 at 18:03

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.