pre_get_posts
runs on every query, so you want to check if is_main_query()
so it only applies to the default query and not additional queries you run on the page.
You should also get in the habit of referencing the query object passed to the function like $query->is_category()
, as calling is_category()
is a wrapper for $wp_query->is_category()
, which may also have unexpected results depending on what you’re trying to do.
Related Posts:
- Query posts if meta key starts with
- How to fix pagination for custom loops?
- AJAX with loop filtering categories
- Remove the Homepage Query
- Is `query_posts` really slower than secondary query?
- Are there any scenarios where the query_posts may be used?
- Transient not working for custom loops
- pop-up lighbox with AJAX
- post loop causes wp_users and wp_usermeta DB queries for each users
- get_the_foo() in the loop – does it perform another query?
- If No Search Results, Show Posts from Alternate Search Query
- Exclude filter on front page
- How to Loop within a Loop (Display Children and then Grandchildren)
- Loop posts only excluding first post
- Filtering The Loop For Single Page and Blog Page
- Sort Posts By Category?
- Custom search form to display users only
- Loop after page content
- How to get an array of years from all of the search results, and use it to filter by year the paginated loop?
- A loop with all articles with certain text strings in tags
- Display most viewed post from last 30 days is not working correctly
- 3 Posts in Loop, Show Stickies First
- How to return only one instance of each, from the entire loop
- How to go about combining dropdowns / filter queries?
- Query last updated posts (posts updated in the last 24 hours)
- Combining action and filter?
- Filter the loop by categories using checkbox form
- Conditional query that displays on Woo product attribute pages
- How do I use AJAX Query in my posts loop?
- woocommerce get_price_html not pulling in correct price
- Filter the_posts doesn’t work on search page
- How to fix pagination for custom loops?
- Default ‘post’ post type archive slug?
- How can I paginate this?
- How to display posts by vote count and if no value continue with latest posts with no votes?
- How to fix pagination for custom loops?
- Split wordpress loop to multiple layouts
- Making a query to the DB using same parameters of loop
- Loop inside query
- Trying to get property of non-object in shortocde
- Make a loop to return x number of posts, but only if they have content or excerpt
- get_delete_post_link() inside Loop stubbornly returns nothing
- loop inside the foreach
- Query posts only with actual text content (not including shortcode or images)
- Filter existing hook using parameter and foreach loop
- Exclude posts based on an array
- Query post & loop problem.
- Get current taxonomy and display query accordingly
- Loops for cat links not looping for wordpress site
- How do I set the Stewart Search Template to return only published posts in the result
- Loop order issue with Ajax filter
- Trying to get all links in my posts
- Custom Query with Sticky Posts
- Add Search and Filter functionality to custom loop
- Build a Page which shows the Oldest Post with specific Custom Field Value
- Reorder posts in a loop: have the posts by one particular author below the others
- Query posts and display all dates in repeater field in chronological order
- How to filter get previous post function by meta value DESC and post date DESC?
- WordPress redundant posts based on meta field
- How to fix pagination for custom loops?
- Get featured image outside the loop using foreach
- Alternative layout the loop
- Show posts by tags excluding current post
- Remove posts_orderby filter then add it back in
- Strange problem with wp_get_attachment_image_src database (db) query count
- Next 10 posts data of currently viewing post in blog page
- How to fix pagination for custom loops?
- Buddypress Group Activity Loop Filter activity type [closed]
- get_posts and wp_autop (remove filter)
- How to fix pagination for custom loops?
- How to fix pagination for custom loops?
- Get query result before posts are displayed?
- Combine Query and Select Database to create directory loop
- WordPress loop: Display if posts exist
- wordpress showing all posts instead of date range
- Help with add_filter(‘the_content’, ‘some_function’) and multiple matches
- Applying a filter to multiple loops for days ago
- Multiple loop with pagination in same page
- Woocommerce set loop_shop_columns to be 3 in the main shop page loop only and 4 otherwise
- Query Loop Block: possible to restrict just child pages?
- Use Base-URL with Query-Loop
- Specific Loop For 2 Within Each
- Check if date of post is yesterday
- Use get_post_types to query only custom posts types
- query_posts() ALWAYS displays something?
- How to get list of posts from permalinks?
- Make loop alphabetical
- Add custom post type to query
- Can’t limit posts_per_page in loop
- How to append the excerpt to the content in the single post page?
- Can’t print out returned value
- Home page loop with pagination problem
- Way of getting queried loop before the query with a filter hook?
- WooCommerce custom loop pagination on front page
- I want the first post on my home page to be lengthier than the rest (example included)
- Loop with Custom Post Type Taxonomies and Interstitial Code
- Question about custom plugin
- How can you make it so the comment box shows on some pages or posts but not all?
- wp_mail sending only once inside foreach loop
- How to use orderby on meta_value when using Pods custom database table storage