Update:
This question isn't really relevant anymore. I switched some code, so now the filtering works.
Looks like the query wasn't working, because I triggered the indexTaxonomyCounts
function from a template?
But I now have another problem, but will write a different question for that.
Original question:
I want to filter my custom products by some taxonomy and meta filters. This works for regular queries, but not for my search results. At least, the filtering works, but getting the right counts somehow does not.
What I do is the following:
I hook in to the
pre_get_posts
hook and use the data from the $_GET to create taxonomy and meta queries.Then I store the query I just created (including the filtering), so on the result page I can calculate for each taxonomy/meta filter, how many results there totally are.
But somehow when I run the stored search query again (but now with posts_per_page=-1
) I get no results? No idea why. Does the search perform different queries then regular queries?
Maybe it's more clear with some code snippets.
The following function works, the search results are filtered and all:
add_action('pre_get_posts', array($this, 'pre_get_posts_hook'));
public function pre_get_posts_hook( $query_obj )
{
//check if this isn't the internal query, since that doesn't need filtering ^^
if( empty($query_obj->query_vars['internal-query']) || $query_obj->query_vars['internal-query'] != true ) {
$tax_query = array();
$meta_query = array();
if(isset($_GET['someMetaFilter']) {
foreach(explode(',', $_GET['someMetaFilter']) as $tax_value) {
$meta_query[] = array(
'key' => urldecode($tax_value),
'value' => array(''),
'compare' => 'NOT IN',
);
}
}
if(isset($_GET['someTaxonomyFilter']) {
foreach(explode(',', $_GET['someTaxonomyFilter]) as $tax_id) {
$ids[] = urldecode($tax_id);
}
$tax_query[] = array(
'taxonomy' => $type,
'field' => 'term_id',
'terms' => array_map('intval', $ids),
'include_children' => false,
'operator' => 'AND'
);
}
if(count($tax_query)) {
$tax_query['relation'] = 'AND';
$query_obj->set('tax_query', $tax_query);
}
if(count($meta_query)) {
$meta_query['relation'] = 'AND';
$query_obj->set('meta_query', $meta_query);
}
//only set it if we have set a specific filter set
if(count($tax_query) || count($meta_query)) {
self::$query_vars = $query_obj->query_vars;
}
return $query_obj;
}
This function is called from the result-page, to calculate all the counts for the taxonomies. But this just results in 0 results :S
The only thing I do is getting the previously used query_vars and set the post_per_page
to -1 (so I can get all the results, instead of only for this page)
public static function indexTaxonomyCounts()
{
if (isset(self::$query_vars)) {
$query_vars = self::$query_vars;
$query_vars['posts_per_page'] = -1;
$query_vars['internal-query'] = true; //to prevent the pre_get_posts_hook to adjust the query
$wpQuery = new WP_Query($query_vars);
//somehow in the regular query, this works. But when I do this in a search result page, the $wpQuery->have_posts() is empty! But the result page does have 30+ results or something :S
// The Loop
if ( $wpQuery->have_posts() ) {
while ( $wpQuery->have_posts() ) {
...
do some calculations based on the taxonomy and such
...
}
}
/* Restore original Post Data */
wp_reset_query();
}
}
I tried so many things, I don't even know what anymore. I copied the query_vars, I copied the WpQuery. I tried to reconstruct the WpQuery by adding the ?s=$_GET['s']
or something. But to no result.
So if anyone knows what's the cause of this, I would be so grateful!
Additional information (1)
Ok, I did some logging (using the posts_request
filter and I have 2 dumps of data.
It's way too big to dump it here.
So I uploaded them to my Dropbox:
This one works: https://www.dropbox.com/s/a07defqjx1d95j0/regular-page.php
And this one does not: https://www.dropbox.com/s/zboiw4fjqojotw7/search-page.php
As you can see in the code snippets above, I just use the same query_vars. The two query_vars differ only slightly, but I'm not sure what's causing that.
The SELECT * FROM mug_posts WHERE 1=2
is weird, but the search does give back results - so I figured that search does some other query???
Additional information (2)
Ok, it's getting even weirder. I simplified the logic, I stripped out the whole taxonomy filtering and such. In the pre_get_posts
I store the query and on a page I call a function, which checks that stored query and runs that again. But I get no results back!
public function pre_get_posts_hook( $query_obj )
{
if( $query_obj->is_page() == false ) {
if( $query_obj->query_vars['post_type'] == 'products' ) {
if ( ($query_obj->is_main_query() == false && $this->filterIsActive())
|| $query_obj->is_search()) {
self::$query_vars = $query_obj->query_vars;
}
}
}
return $query_obj;
}
public static function indexTaxonomyCounts()
{
if (isset(self::$query_vars)) {
$query_vars = self::$query_vars;
$wpQuery = new WP_Query($query_vars);
echo $wpQuery->have_posts(); // -> false
/* Restore original Post Data */
wp_reset_query();
}
}
I printed the query_vars for a regular page and for the search page and they are almost identical:
Query vars on a regular page: https:// www . dropbox.com /s/nxy75avclwtr5xt/results.php
Query vars on a search page: https:// www . dropbox.com /s/g72nhsfwej9q95c/no-result.php
(sorry for the weird link, but I don't have enough reputation yet to post more links)
You'd think since the query vars are (almost) identical, they'd give the same result. Nope :P
echo 'xxx'
within the internal-query conditional. Is it possible that that if statement is not being fired, so it's leaving out something?posts_request
and echo the SQL out. Run it in MySQLWorkBench or something like that, and see if it gives you an error - could be as simple as a syntax issue.