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' );
Related Posts:
- Chrome net::ERR_INCOMPLETE_CHUNKED_ENCODING error
- cURL 28 error after switch from to brew php 7.2 on localhost
- How to run multiple Async HTTP requests in WordPress?
- WP CLI info showing correct PHP binary but wrong version of PHP
- Does the debug.log do log rotation?
- apache cpu over 70% on localhost
- Is it possible to move wordpress out of webroot?
- login wp impossible
- wp-admin/index.php gives a “500 Internal Server Error [closed]
- Memory errors with media upload, WordPress can’t use more than 96M (while there’s 512 available!)
- Evaluations of two wordpress security plans against php code injection attack
- WordPress (3.9.1) MultiSite Permissions. Is chown the answer?
- Is XAMPP faster than running LAMP in WSL on Windows 10? [closed]
- Apache /Ubuntu server not running WordPress installation, outputting PHP code like HTML
- Apache Fallback instead of add_rewrite_rule
- PHP Warning: chmod(): Operation not permitted in class-wp-filesystem-direct.php on line 173
- WordPress admin dashboard missing icons
- WordPress returning 404 for multisite pages
- how to combine wordpress htaccess on my root domain + php on subfolder
- How to disable server signature on wordpress? [closed]
- Posting and image insertion problems after EasyApache4 and PHP 7 upgrade
- Call to undefined function mysql_connect() After upgrading PHP 7.1 to 7.3
- How does WP work in conjunction with a web server?
- Permalinks are not working in WordPress in digitalocean
- Apache HTTP Server stops working for only a certain local website
- Problem with data collection in tables
- How to edit content in WordPress and the Polylang – plugin? – with demosite
- Upgrade to PHP7.3 and Changing Apache from Prefork to Event Breaks WordPress
- Windows Setup: Error establishing a database connection
- Huge time to first byte on live site
- WAMP SERVER Command Prompt SET PATH=%PATH%
- 403 Forbidden Localhost Wamp Apache Php
- Strange special character/Latin characters
- Rewrite /keyword1+keyword2.html to search page | .htaccess
- Not logged in when using http
- Admin-area broken through weird issues
- Blog only showing code
- WordPress Memory limit not increasing
- Having an HTTP error 500 after migrating a website
- session_start(): Cannot find save handler ‘mm’ – session startup failed in /sites
- how to put shortcode to the top of the page – theme 2014
- How to avoid timeout waiting for output from CGI script?
- How can I prevent wordpress from sending emails
- Restricting access to a file for everyone except logged in users
- Setting $_SERVER[‘HTTPS’]=’on’ prevents access to wp-admin
- Check if a menu is empty?
- Adding a search form inside a div
- How To Make Connection To WordPress Data Base In A Plugin?
- Dynamically change feature image in customiser
- Walker class: Problems with understanding how walk() method can be called without error
- How to get the post count for the last x days filtering by categories
- How to create shortcodes that pull custom field data from general settings
- Getting a fatal error while updating
- How to display related posts from parent category
- Using is_page() in functions.php not working at all
- #1062 – Duplicate entry ‘1’ for key ‘PRIMARY’
- Warning when using
- How to override wp-admin styling
- How to call function at the bottom of post using plugin?
- How can I resolve the php notice “Constant EMPTY_TRASH_DAYS already defined”
- Use wp_get_recent_posts with search term
- Choose To Display Post Views With An Options Panel
- Prevent Data Resubmission On Page refresh
- Getting trackpacks/pingbacks for a post via wordpress?
- wordpress allow user to edit user profile with custom fields
- Making BuddyPress Activity Stream (Post Type) Featured Images Clickable [closed]
- Which field should I edit to make the checkbox marked by default?
- Add filter when image is uploaded?
- Keyword checking in Gravity Forms
- Get URL Parameters from referer
- Adding logo to WordPress header
- add_filter only for front office
- WordPress and MySQL: trying to print data using PHP from user_meta custom field data
- Form Submission Not Working In Custom Theme
- Show metabox only for homepage
- Concatenate Custom Field Value & HTML Value
- How do I properly code my footer? See example
- Use of IF statement in Header file
- Advanced Query posts
- Triggering a timewall after X days
- Bootstrap 4 Carosuel with ACF Repeater field
- How to preload header logo image in WordPress? like what’s the code and where do I put it?
- PHP `iconv` not working inside a WordPress site
- How to make a “page-option” in WordPress
- Replacing entire tag with shortcode – JavaScript [duplicate]
- Use PHP to inject HTML into before page renders?
- Query doesn’t display text data with apostrophes
- WooCommerce Custom Tab with ACF Repeater Field
- My WP Website Shows HTTP ERROR 500
- soap request in wordpress for a fedex pickup request [closed]
- Dynamically Create Posts Via XML File
- Create server side text from wordpress page
- Make first child link default instead of its parent
- How to set a cookie based on a page Get variable?
- Editing the contents of just one div on a page from wordpress
- How to change the menu color
- Display latest 5 posts on homepage
- I want Page titles and excerpts to show up on home.php in certain order
- Advanced custom fields Repeater conditional statement not working [closed]
- how to display tables via ajax