I think the solution is to check if it’s the main query. WordPress has a function for that, is_main_query()
The query that you’re running in your related posts widget is a secondary query, so is_main_query()
will return false.
You can take advantage of that, by changing your code like this:
if ( ! is_main_query() || ! is_singular() )
return $content;
Related Posts:
- Remove option to allow trackbacks/pingbacks from post page options
- Display posts by month
- How to get 2 or multiple custom post types in wordpress functions.php
- Creating bulk posts with Youtube videos
- Hide wordpress field if data is empty in post!
- Custom posts in different columns style
- What file have I to create in my custom WordPress theme to show all the post belonging to a specific category?
- Update if post with specific title exists
- Auto create description in post
- Add multiple HTML attributes to an Elementor button
- How to update custom fields using the wp_insert_post() function?
- Why have on every line
- WordPress and event-driven programming – what is it about?
- Admin Bar (Toolbar) not showing on custom PHP file that loads WordPress
- Set the transport of the Customizer ‘header_image’ core setting to ‘postMessage’
- Limit the Excerpt field in WP-Admin in words
- delete an array element when its date expires
- New Plugin Review
- Use template for posts with a particular category grandparent
- “before delete post” action fire when the post is updated?
- Random Default Avatar Function
- Automatically add custom CSS to new posts using a category template
- Query all posts of a custom taxonomy term
- Customizer: Output default value in Customizer CSS
- Taxonomy linked to pages
- Displaying the last post on static homepage
- Create another “Display Site Title and Tagline” checkbox, “Header Text Color” setting and control
- Can’t get wp_insert_post to work
- Why not yield/iterable in posts interface?
- Converting HTML Template to WordPress Theme
- WordPress Custom Fields (Checkboxes with multiple values)
- Can I install/embed WordPress on a ‘single page’?
- 200 Rewrite blog post links
- Automatically create child pages and grandchild pages when saving a (parent) page
- How can I add extra word in permalink when someone click download button?
- Using $wpdb (WPDB class) ‘replace’ with multiple WHERE criteria problem
- 404 error when I acess the second page of post-type
- How do I add a custom css to all posts without affecting homepage css? [closed]
- Which PHP page is the Default Posts Page
- Advanced Custom Fields not displaying
- retrieve post data and insert it in seperate static html page without WordPress
- Exclude posts with specific metadata from search?
- Copy taxonomy terms from one post to another programmatically
- Add data-id attribute to child page links
- Display Youtube Time Automate from Key
- How to fix this PHP warning in WP-Admin after upgrading to 3.1.2?
- How to catch wordpress post ID when it’s published
- Find Site ID From WP_Post
- Where is the PHP code generating an element?
- Basic wpdb update question
- Check class_exists before class definition / Doxygen problem
- WordPress upload file size error even after raising php limits
- WordPress widget and customize.php not working in Ubuntu16.04 VPS
- How can I get “Previous” and “Next” to show in the navigation besides the links?
- How do i wrap woocomerce attribute in list?
- Is it possible to add a custom page to a free WordPress site? [closed]
- save metabox with new values _wp_page_template
- Saving a post ID to use in a form elsewhere on website
- How to Fix an Archive.php That Displays All Posts?
- pagination broken – clicking next displays “page not found”
- Create condition for Author bio Social Links
- remove specificly the last tag in all posts
- Add posts to custom table in database instead of wp_posts
- How to show categories and date on posts
- Woocommerce template file outputting tags
- Trying to update Woocommerce meta values
- If Statement is Ignored?
- Query on a repeater date (acf)
- Trying to show the category of a post in the post display
- Split post or page content in two or more columns
- Check the database for a postmeta field
- Create “blank” admin page without having admin-bar/admin-menu for faster load
- Send POST request to Flask app from functions.php file
- Create Schema in WordPress with PHP
- Send Notification after post published was working but stopped after the last WP update
- How to insert text into post based on category – via single.php file
- Add wp posts to cutom masonry grid
- Generate random access token to a post page
- How to associate dynamic PHP page for chosen WordPress tag?
- Is there a way to have chunks of a page’s content expire?
- Where can I find the declaration of `$_wp_theme_features`?
- Static home page ignored
- How do I routinely extract the thumbnail of the most recent post?
- Showing the project type in HREF
- ‘posts_per_page’ in costum page type
- Display tab title as ‘blog tagline | blog title’
- My customizer’s setting doesn’t set to the default and needed to click the control’s “Default” button before it’ll be set
- List of child pages fetch next results at link click
- Showing custom field contents without listing description
- Check if values exists DB
- Pagination on Custom Loop
- Show a Category Specific Info Box
- error with WP custom form
- MySQL Engine vs. Charset
- Select area and checkbox data is not saving?
- How to modify this function to exclude also the post belonging to a specific category?
- In reading settings posts are set to “blog” and I have page-blog.php but no posts
- Set post to draft if matching word is found in the post
- WP Custom tables query
- Disable single posts, but keep archive