This is an old version of this answer!
Return to the current answerHi Adam:
Unfortunately I don't have appropriate test data for this so I can't give you tested code but I think I can get you to approach the problem differently with the result being that your problem will disappear, at least I assume it will.
If you look at your code for the brand-specific branch of the if..else you'll see you are calling two queries: first get_posts() and then query_posts(). However you use the paged parameter on your first query and never use it on the second query. I believe that is the crux of your problem.
Obviously the reason you've used two queries is because there is no support of multiple meta keys in what I'll call the "WordPress Query Language." So you ran one query to collect up all the IDs and passed the paging variable to it and then called the second query without the "paged" parameter and that is the query that runs your page results. You could potentially move the "paged" parameter from get_posts() to query_posts() and it may solve your problem. However, for a large data set using two queries is a really inefficient way to do things anyway (but understandable it was the only obvious way to do it, and kudos to you for knowing how to make that work, most people wouldn't have a clue.)
Let's tackle this with another approach? Let's add a "posts_where" hook that lets you use the following single query without needing an if..else; just change the query:
Here's the filter hook:
I've created a standalone example you can drop into the root of your website to play with this. You can find it here: http://gist.github.com/501033
Let me know if this helps.
UPDATE: I see someone else answered and you had a different problem. I'd still recommend you look at using the filter hook instead of multiple queries. FWIW, I'm not concerned about the $20; if you would have given me some just give it to others.
Unfortunately I don't have appropriate test data for this so I can't give you tested code but I think I can get you to approach the problem differently with the result being that your problem will disappear, at least I assume it will.
If you look at your code for the brand-specific branch of the if..else you'll see you are calling two queries: first get_posts() and then query_posts(). However you use the paged parameter on your first query and never use it on the second query. I believe that is the crux of your problem.
Obviously the reason you've used two queries is because there is no support of multiple meta keys in what I'll call the "WordPress Query Language." So you ran one query to collect up all the IDs and passed the paging variable to it and then called the second query without the "paged" parameter and that is the query that runs your page results. You could potentially move the "paged" parameter from get_posts() to query_posts() and it may solve your problem. However, for a large data set using two queries is a really inefficient way to do things anyway (but understandable it was the only obvious way to do it, and kudos to you for knowing how to make that work, most people wouldn't have a clue.)
Let's tackle this with another approach? Let's add a "posts_where" hook that lets you use the following single query without needing an if..else; just change the query:
query_posts(array(
'post_type' => 'post',
'meta_key' => 'Retail_Price',
'orderby' => 'meta_value_num',
'meta[product_brand]' => (isset($_GET['brandName']) ? $_GET['brandName'] : ''),
'posts_per_page' => -1,
'order' => (!isset($_GET['order']) ? 'ASC' : strtolower($_GET['order'])=='asc' ? 'asc' : 'desc')),
));Here's the filter hook:
add_filter('posts_where','multiple_meta_criteria_posts_where',10,2);
function multiple_meta_criteria_posts_where($where,$wp_query) {
if (isset($wp_query->query)) {
$query = $wp_query->query;
if (is_string($query))
parse_str($query,$query);
if (is_array($query)) {
global $wpdb;
foreach($query as $param_key => $param_value) {
if ($param_key=='meta') {
foreach($param_value as $key => $value) {
if (!empty($value)) {
$sql = " AND {$wpdb->posts}.ID IN (SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key=%s AND meta_value=%s) ";
$where .= $wpdb->prepare($sql,$key,$value);
}
}
}
}
}
}
return $where;
}I've created a standalone example you can drop into the root of your website to play with this. You can find it here: http://gist.github.com/501033
Let me know if this helps.
UPDATE: I see someone else answered and you had a different problem. I'd still recommend you look at using the filter hook instead of multiple queries. FWIW, I'm not concerned about the $20; if you would have given me some just give it to others.
Mike Schinkel | 07/30/10 at 2:47pm





