2

I want to replace the main query with my own custom query. The thing is I don't want to go through the hassle of manual paging. I know there's a filter posts_results but I'm not sure how to use it exactly, I couldn't find a bunch of info on it. It looks like pre_get_posts is only altering the query instead of replacing it entirely. I tried to just dump my query into $posts but then I lost paging. Is there a way around this?

I'm using $posts = wpdb->get_results($query); to replace the main query with my own.

$query is my custom SQL. I'm adding the snippet above right under get_header(). I'm trying to edit the main query on my custom taxonomy page: taxonomy-mytax.php.

The Situation

Currently my client has Manufacturers (Taxonomy) and Products (Taxonomy) then an actual Product (Custom Post Type). They requested to show Product posts (on the product category page - taxonomy-producttax.php) in an order by their most favorable Manufacturer (taxonomy). So I added an extra field into the manufacturer taxonomy where they can set a number that declares an order. This number is saved into $wpdb->terms.term_group. Now on the Product Category page I'm trying to replace the main query entirely with a brand new query that will get all the products and put them in order by a manufacturers term_group. Both Product and Manufactuers (both taxonomies) have an order, and both are put into $wpdb->terms.term_gruop which was troublesome. I created a custom query that works, and will show posts in manufacturers term_group order . I just need to replace the main query on taxonomy-producttax.php with my custom SQL query, but I didn't want to get into manual paging I wanted to use anything Wordpress has built in to help me.

  • Please edit your question to clarify/specify how you want to replace the main query. What is the context? What is the main query? And what do you want to replace it with? – Chip Bennett Nov 27 '13 at 15:59
  • @ChipBennett The code snippet above is pretty much how I'm changing the main query. I'm dumping my query results into the global $posts variable so that I can still use The Loop - but it breaks paging. I'm adding it right under get_header() – Howdy_McGee Nov 27 '13 at 16:01
  • I will reiterate Chip's questions: What is the context? What is the main query? What type of page are you attempting this on- a page, an archive of some type? – Milo Nov 27 '13 at 16:12
  • I'm using it on a custom taxonomy page template. I guess I don't see how it matters since I'm seeing the answer being the same if I were on a post page or archive, or page since all have a main query and that's what I'm trying to replace with my own custom query without losing paging. – Howdy_McGee Nov 27 '13 at 16:14
  • it wouldn't be the same, paging works differently on a page vs an archive. WordPress selects the template based on the results of the main query. if you're trying to load a paged archive and the main query is empty for the requested page number, you'll get the 404 template. this won't happen on a page though, the main query will always contain the requested page data. the simple answer to this is- don't try to do this, it is fraught with many potential issues. if you can be more specific about what your query does, we may be able to help you modify the main query before it's run. – Milo Nov 27 '13 at 16:20
3

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.

  • @Howdy_McGee : see the edit – s_ha_dum Nov 27 '13 at 16:42
  • So from what your saying is I can't replace the main query and still keep paging, right? I would have to do paging manually pretty much. – Howdy_McGee Nov 27 '13 at 16:43
  • You can create a custom Loop and paginate it, yes. I think that is what @ChipBennett is referring to and the link he provided should help with that. It is pretty much always less efficient that modifying the main query and is tricky to get right, but a lot depends on the fine details. That is an option to consider but isn't strictly what you are asking about. – s_ha_dum Nov 27 '13 at 17:39
  • So from the looks of it, I can dump $wpdb->get_results into $posts and still use The Loop but I just need to figure out the offset and limit before I execute the query. – Howdy_McGee Nov 27 '13 at 18:34
1

Well it is a taxonomy archive page. What my query does is order posts by a category order that the users sets... So I would need to change the select, the join, the where, the order.

Without seeing your actual code, it's difficult to give a more-precise answer. However, besides the pre_get_posts action hook, WordPress provides several filters for manipulating the query even earlier:

There is not likely any real reason that you can't manipulate the main query to suit your needs:

So I added an extra field into the manufacturer taxonomy where they can set a number that declares an order. This number is saved into $wpdb->terms.term_group. Now on the Product Category page I'm trying to replace the main query entirely with a brand new query that will get all the products and put them in order by a manufacturers term_group. Both Product and Manufactuers (both taxonomies) have an order, and both are put into $wpdb->terms.term_gruop which was troublesome. I created a custom query that works, and will show posts in manufacturers term_group order .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.