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 Change Order of Posts in Admin?
- Get first post from wp_query
- Accessing the post content with WP_Query
- How to query serialized array by comparing greater than (>=)?
- Query posts distinct authors
- How to get previous 10 days post from a specific date – WP Query
- Specific loop in Shortcode
- Sort posts by activity date
- prevent showing posts of an specific category in admin posts section
- How to output comments number of a post per day?
- Search widget breaks when using multiple loops?
- How to change bulk post status
- If specific user role then sticky post
- Display posts with comments closed, with pagination?
- posts_per_page doesnt work
- I want to query posts in monthly basis
- Custom WP_QUERY $args
- Improving WP_Query for performance when random posts are ordered
- Why won’t my taxonomy query show up?
- Limiting the number of posts in WP_Query leads to unexpected result
- pre_get_posts query between 2 dates (date stored in custom post meta)
- WP_Query with ajax handler returns the same posts
- Why WP_Query(‘showposts=5’) shows only 1 post?
- How to insert 2 args into 1 Wp_Query for a slideshow
- Blog page pagination is not working after using the offset argument
- WP Query – Posts Per Page not working in combination with category__in
- how to change the number of posts returned in a specific loop without making it global?
- posts_per_page option limits the number of Gallery items
- Nested WP_Query breaking loop
- 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
- Displaying post excerpt using wpdb query
- Query for first 3 posts to change the look and feel
- Getting blog pagination to work on page set as front page
- search blog posts by author first name and or last name
- 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
- How to give classname to post if post has no content?
- Custom wp-query in wordpress rest api
- posts_per_page returning only one post
- Need to know custom code to display random and most viewed posts in wordpress posts and pages
- How to retrieve certain number of images from a wordpress post?
- WordPress post filter menu
- How to display post list in a table layout (multiple queries in single loop)
- Not all posts showing in query
- WP_Query custom field pass the post id
- WordPress query portfolio posts
- Current and next month posts
- Get latest author posts inside the loop
- Same post appears in related Posts?
- Display 3 posts with different HTML markup using a loop
- query for filtering published posts?
- How to show multiple posts thumbnail, title and date in widget
- WP_Query outputs wrong post in custom post type
- how to show comments only author which send own posts in wordpress
- Group first, 2nd, 3rd, etc posts by category terms
- Manipulated offset and pagination, can’t display last post anymore
- How to sort WP_Post Object array by object field in php?
- Order by meta_key field in WordPress not meta_value field value
- How to display particular set of wordpress post on a webpage?
- How to create page with post content in it?
- Top tags with minimum number of posts
- How to remove a post from results by ID after query?
- Show 5 posts and than 3 posts offset with pagination
- Override wp_link_pages pagebreak with filter
- WP_Query extended with AJAX
- Is it possible to query a category with specified posts?
- Blog only showing one post even though more published
- WordPress loop uses unmodified posts array, why?
- Sort / Filter Queries
- Show posts between two Custom Fields date
- Query to get data of a post, if in category?
- Order posts by custom field
- Why I have this strange behavior when I try to exlude the featured post from posts visualization?
- How do I control the fallback query after the original query returned cero posts?
- Remove duplicated posts in the loop if post has more than one category
- Order posts by separate menu order for different sub-categories
- Adding a category at even positions on main loop with modified pagination
- Why I getting only 1 post?
- What is wrong with this wp_query?
- Exclude sub sub categories from displaying in the archive page
- First post in loop displays twice
- How to var dump urls for all posts
- How do I get content of custom post type through post ID