Reputation: 447
I have a used vehicle site. When viewing a specific vehicle, there is a section for similar vehicles. Currently, the similar vehicles are based on the vehicles with the same body style ($vehicletype).
I would like to change it to display vehicles of the same body style with a similar price. So far I've got this code working:
$vehicletype = get_post_meta($post->ID, 'Vehicle Type', true);
$id = get_the_ID();
$baseprice = get_post_meta($post->ID, 'Retail Price', true);
$minprice = $baseprice - 10000;
$maxprice = $baseprice + 10000;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => array('vehicle'),
'post__not_in' => array($id),
'post_status' => 'publish',
'order' => 'DESC',
'orderby' => 'date',
'posts_per_page' => 5,
'paged' => $paged,
'meta_key' => 'Vehicle Type',
'meta_value' => $vehicletype,
'meta_key' => 'Retail Price',
'meta_value' => $baseprice,
);
This obviously displays vehicles with both the same body style and the exact same price. I would like to change this so that it display not just vehicles of the exact same price but vehicles within similar price range (between $minprice and $maxprice).
I suspect it would look something like this:
'meta_query' => array(
array(
'key' => 'Retail Price',
'value' => array( $minprice, $maxprice ),
'type' => 'numeric',
'compare' => 'BETWEEN',
),
),
I have tried several different ways to get this working, but all of them cause a "This page isn’t working" error page.
Any help would be appreciated.
Thanks in advance
Willem
Upvotes: 1
Views: 60
Reputation: 1354
Try something like this is the between is not working:
'meta_query' => array(
'relation' => 'AND',
array
(
'key' => 'Vehicle Type',
'value' => $vehicletype,
'compare' => '='
),
array
(
'key' => 'Retail Price',
'value' => $minprice,
'type' => 'numeric',
'compare' => '>=',
),
array
(
'key' => 'Retail Price',
'value' => $maxprice,
'type' => 'numeric',
'compare' => '<=',
)
)
Upvotes: 1
Reputation: 2971
Try this
$vehicletype = get_post_meta($post->ID, 'Vehicle Type', true);
$id = get_the_ID();
$baseprice = get_post_meta($post->ID, 'Retail Price', true);
$minprice = $baseprice - 10000;
$maxprice = $baseprice + 10000;
$query = array();
$queryPrice = array();
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
if( "" != $vehicletype )
$query[] = ['key'=>'Vehicle Type','value' => $vehicletype, 'compare' => '='];
if( "" != $baseprice ){
$queryPrice[] = ['key'=>'Retail Price','value' => $minprice, 'compare' => '>'];
$queryPrice[] = ['key'=>'Retail Price','value' => $maxprice, 'compare' => '<'];
}
$args = array(
'post_type' => 'vehicle',
'post__not_in' => array($id),
'post_status' => 'publish',
'order' => 'DESC',
'orderby' => 'date',
'posts_per_page' => 5,
'paged' => $paged,
'meta_query' => array( 'relation' => 'OR ',$query )
'meta_query' => array( 'relation' => 'AND ',$queryPrice )
);
$wp_query = new WP_Query($args);
Also, your query have
'meta_key' => 'Vehicle Type',
'meta_key' => 'Retail Price',
please make sure you provided key is what is in database
Upvotes: 1