Searching for posts with custom data in a numeric range (price)
-
Hi
I’ve been stuck on this for ages and am hoping someone can help me as I am not a programmer!
I have a website where i use posts as products and i store additional data about the product, such as price in custom fields.
I have set up the searchform.php to include several dropdowns, for selecting category, tag and price range in addition to the keyword text search to allow better searching for the products. The dropdown selections are being added to the URL or blank if not selected,
An example would be: https://www.mysite.com/index.php?category=mycategory&tag=&price=10-20&s=+In my search.php template I am using the query_posts and customising it for the additional dropdown selections, but it seems i can’t use the meta compare for the price custom data, as I want to do a between range and also the prices are stored as strings not numbers.
I’ve been searching around and in a roundabout way gleaned that I may be able to use the posts_join and posts_where and include some sql for the price bit of the query with a CAST, but not being a programmer I have tried and failed, producing all sorts of undesirable results.
Can anybody tell me if I am along the right lines with this and quite how I go about it, or another way of searching for posts in a price range.
Many thanks.
This is what I have for building up my new query_post statement if this helps clarify what I am trying to do?
<?php $selectedCategory=''; $selectedPrice=''; $newsearch=''; $newsearch=$query_string; $newsearch.="&posts_per_page=14"; $newsearch.="&post_type=post"; $newsearch.="&author=3"; if(isset($_GET["category"]) && $_GET["category"]!='') { $selectedCategory=$_GET["category"]; $selectedCatId = get_term_by( 'slug', $selectedCategory, 'category' ); $selectedCatId = $selectedCatId->term_id; $newsearch.="&cat=".$selectedCatId; } if(isset($_GET["price"])&& $_GET["price"]!='') { $selectedPrice=$_GET["price"]; $parts = explode("-",$selectedPrice); $pricemin = $parts[0]; $pricemax = $parts[1]; **how do i add price to the new query string?*** } query_posts($newsearch); ?>
- The topic ‘Searching for posts with custom data in a numeric range (price)’ is closed to new replies.