The pre_get_posts
function is a simple query modifier for general “post” queries. If you’re looking to modify the SQL directly for these query I suggest using the post_clauses
hook. This hook passes an array of SQL clauses such as:
$sql['where']
and sql['join']
Here you can inject your own custom SQL into the specific clauses. That being said, if you are truly writing something custom it might be beneficial to circumvent WordPress entirely and just use $wpdb
to grab your results.
Related Posts:
- When should you use WP_Query vs query_posts() vs get_posts()?
- When to use WP_query(), query_posts() and pre_get_posts
- Wp get all the sub pages of the parent using wp query
- How to query for most viewed posts and show top 5
- WP_Query vs get_posts
- Changing the meta_query of the main query based on custom query_vars and using pre_get_posts
- How to uniquely identify queries?
- pre_get_posts with get_posts
- Query Custom Meta Value with Increment
- Get posts by meta data OR title
- Perform query with meta_value date
- Using is_main_query to select custom post type on certain page
- WP_Query ordered by custom field that is a date string?
- Can not switch the queried post in pre_get_posts hook
- Pass the same object to multiple widgets in a template with one query
- Splitting the main query in multiple loops with query_posts and/or pre_get_posts?
- Retrieve or Query Pages by ID
- How do you query wordpress posts using a math formula between multiple meta field values?
- How to implement time filter to show random post 1 month for one category and 3 months for other categories
- Page navigation doesn’t show when query category
- Using WP_Query in “parse_query” or “pre_get_post” in Posts2Posts
- How to query a custom post type with a taxonomy filter but display post type archive page?
- Store sticky post’s ids in a transient
- Change query_posts to WP_Query in page but does not work
- Custom Query num_rows returns wrong amount
- Slow page loads due to WordPress Core Query
- Filtering out child category posts from parent category archive not working
- Query Page Content From Theme Options?
- How can I override one post and make it display content for another post?
- Include current post into loop
- pre_get_posts – Trying to get property of non-object warning
- Filter post query to only show direct children of category
- Should I reset $wp_query?
- Why doesn’t my WP Meta Query return any results?
- Elementor posts custom query based on tag [closed]
- `offset` WP_Query argument dont work via `pre_get_posts`
- Filter products on category AND tag
- Get posts that match defined arrays of tags
- I need query_posts() to order results first by a meta value and then by post ID
- How to show post title in content editor in backend?
- My entry results are not consistently alphabetized
- Merge two queries and remove duplicate
- Using the same WP_Query for shop and widgets in WooCommerce shop
- Modify query to exclude certain ids and certain parents and its corresponding children
- Ordering Posts by parent category, name ascending
- wp_query – Modify $query to include duplicate content
- Optimising specific Query with ACF meta objects
- How to organize a WP_Query’s list of posts by category and display category title?
- When should you use WP_Query vs query_posts() vs get_posts()?
- How to query post ids liked by the Author
- WordPress Query showing multiple titles
- Query Posts by date range with fixed beginning and end
- custom query to get posts
- Custom post types loop on a page template
- Pre_get_post on CPT archive page
- Search query alteration not working for meta values
- Using OR conditions in meta_query for query_posts argument
- Get posts by menu ID
- Meta Query with date and time on the same Day before given time
- Order posts by tags count?
- Add and in the header while looping over custom query in page template
- How to speed up wp_query, took more 5s to run against 100k posts
- How to sort posts in admin by titles with dd.mm.yyyy format?
- Including only current user’s posts in search
- Exclude posts with empty post_content in wp_query
- Minimize database queries to user tables?
- Query with a meta value inside a given range
- Get all products which have both product category
- unable to retrive parent page information
- Show Sticky Post at the top but do not show again in the loop?
- How to display user order by role
- WP Query related posts by tags
- Order by summing multiple values
- hide custom post types with specific meta key ON admin backend
- Check if loop has any categories?
- How to display an other custom post type in a different custom post type’s archive?
- Query only the posts with a post format of “audio”
- Exclude All Posts Which Do Not Have A Tag Assigned
- Update query for wp_posts and wp_postmeta
- Multiple meta_key ordering with pre_get_posts
- How to apply pre_get_posts to a custom query?
- How do I stop the same post showing multiple times in a archive?
- Doing $wpdb->get_results returns NULL, doing the same query in my DB returns correct value
- How to get meta key list efficiently?
- Why pagination is not working with tax_query param?
- WP_Query & Duplicate entries
- How order by works?
- Query by meta value (add a dropdown of all values)
- query_posts() doesn’t seem to be called in my page
- Query posts in current category but not attachment format
- How to get several fields from wp_query?
- Filter WP_Query output before it is accessed (pre_get_posts)?
- Pagination not working with WP_Query (creates links but no page)
- query_posts with sorting on a custom datestamp
- Divide WP_Query posts by date & post type
- New WordPress WP Query using posts from certain categories
- Avoiding page loop
- WP Query – Show custom posts only if user contain some user meta
- Show number of posts AND number on current page (cannot make it work)
- Advanced Search – Is this possible?