You cannot replace the main query with the output of $wpdb
. The main query is a WP_Query
object. $wpdb
returns a simple array or object, but not a WP_Query
object.
Even if you could shove raw SQL into a query (something in the back of my head makes me think you can but I can’t swear to it) it is not advisable. Try var_dump($wp_query);
and you will see that there is a lot going on besides just a SQL query. For the object to work–including the pagination that you want– all of that other stuff has to be set correctly.
You do need to use pre_get_posts
and perhaps other query filters like posts_where
to alter the WP_Query
object, not replace it. Without knowing the details as @Milo and @ChipBennett have requested but you are hesitant to provide, it is not going to be possible to give you much more of an answer.
Related Posts:
- How to exclude latest x posts from a paginated query?
- post__in for get_posts with a dynamic array
- WP_Query to work with custom view
- WordPress Number of Posts Not Changing With posts_per_page
- Custom Order in WP Query
- how to get posts ids inside pre_get_posts filter?
- How to exclude latest x posts from a paginated query?
- How to do set post permalinks using 6 digit random unique function?
- wp_posts table: safely remove unused columns to save database storage
- Why Query is returning empty array?
- meta_value timestamp older than now
- How do I display main query posts in random order using add_filter
- WP_query sort by custom meta_key “price”
- WP_Query post_tilte search in posts table
- How to display all posts divided/ordered by post date? [closed]
- How do I extract just the post ID of the first item in whatever WP_Query returns?
- Show post content and title in diferent divs using WP_Query using a loop
- Why are my paginated posts always returning the same results?
- Ignore latest two posts
- Is it possible to use WP_Query to only pull posts with attachments?
- Show posts by author of membership level (Paid Membership Pro)
- Inner join overrides Advanced Custom Fields plugin’s get_field [closed]
- Every second post different class in blog view
- Loop doesn’t exclude the specified category in home page
- Get posts with multiple categories
- Do not show children of a category
- post__in not working with pre_get_posts, but post__not_in does work
- Display posts of specific category term
- Displaying post excerpt using wpdb query
- Print out last 3 blogposts
- why the same code got different results when using query_posts in functions.php and index.php
- Query posts by Author and/or by Tag
- Query for first 3 posts to change the look and feel
- How to get posts published on the latest date?
- Why posts array is empty?
- Can’t understand why sometimes a [caption] field appears
- Page and post loop same template
- Getting blog pagination to work on page set as front page
- How to group only VISIBLE posts? [closed]
- How to check in functions.php if there is data in a WP_Query?
- Shortcode with ‘year’ parameter
- search blog posts by author first name and or last name
- Show metabox value last post excerpt, title and link
- On what hook can I get the queried object’s final state?
- unique post in “$args” of wp_query display more than one post
- Display current post position in Elementor Posts widget
- Why in my query is display two title?
- Migrate posts from category and sub-category via SQL
- How do I insert a after every 5 posts in a WordPress Loop that infinitely loads posts?
- wp_query with ajax
- How to give classname to post if post has no content?
- Custom wp-query in wordpress rest api
- WP_Query: Show 10 posts in date order, first three random
- How can I insert a new markup element after 4 posts automatically? [closed]
- Exclude some posts from displaying in wp_query based on some condition
- posts_per_page returning only one post
- Modify main query before it run without pre_get_post
- If no posts exist for current day and month, show the next available day of posts
- WordPress Loop: How to display recent posts in multiple divs
- Random posts that always include a particular post?
- Need to know custom code to display random and most viewed posts in wordpress posts and pages
- Related Posts function not working
- How to retrieve certain number of images from a wordpress post?
- WordPress post filter menu
- Custom Post Limit for homepage only without plugin?
- How to display post list in a table layout (multiple queries in single loop)
- Conflict array_splice on loop and query in widget
- Not all posts showing in query
- how to handle the loop using filling bootstrap grid structure?
- Get all custom posts with a certain taxonomy
- WP_Query custom field pass the post id
- How to convert query sql to shortcode in wordpress?
- Link to date_query results in WordPress
- wp_query if have posts show date but not in loop
- Use posts_where to exclude posts ids from wp_query
- Retrieving posts by their date and category
- Query function not executed between element [closed]
- Custom WP_Query not working correctly
- Echo the number of posts being displayed
- When WP_Query only returns 1 post, how do I automatically redirect to that post?
- WordPress query portfolio posts
- Current and next month posts
- WP_Query of Category Not Showing First Post
- remove all custom post created
- Get latest author posts inside the loop
- Limiting post list by taxonomy
- Why does WordPress search returns same number of results for every search query?
- Creating a related posts section in wordpress inside a default post
- Same post appears in related Posts?
- WP_Query articles order by offset in collumns
- How to display WP Query filters?
- Finding a post’s slug
- Display 3 posts with different HTML markup using a loop
- Sidebar limiting to 10 posts?
- Change content off every sixth element
- Is there a function to get post info for any publish_status by post ID?
- query for filtering published posts?
- How Can I Always Display A Particular Post First Using WP_Query?
- Optimal WP Web App Backend for mobile app development
- WP_Query get posts in custom database table [duplicate]