[Resolved] Customising search query
I hope some can help me. I would like to customise the sql which is provided data for the main loop have_post() for search page? I.e not by adding filters as i’ve exhausted those options.
Only other option is to create a custom query page, which i have done but i can’t find a way to get my search submit button to call that page. Is there any way?
Please help? I got a deadline 🙁
Many Thanks, J
I’m not 100% sure what you are trying to do, but if I paste this example into search.php it will return all results but exclude results in cat id 932 -“hats”
<?php global $wp_query; query_posts( array_merge( array( 'cat' => -932 ), //exclude category "hats" $wp_query->query ) ); if (have_posts()) : ?>
thanks David. But my query is complex than that. Let me explain:
I’m modifying the search form, so users can submit a post code along with their search phrase. And i need to display all the posts matching to the phrase ordered by distance. I.e All the posts currently has post code and its geo-coordinates (longitude / latitude)- custom fields. My simplified raw query will look like this:
“SELECT p . * , d.Distance FROM wp_posts p INNER JOIN ( SELECT pm_lat.post_id, “
. “ROUND((((ACOS( SIN( ( 61.4124777 * PI( ) /180 ) ) * SIN((pm_lat.meta_value * PI( ) /180 ) ) + “
. “COS( ( 61.4124777 * PI( ) /180 )) * COS((pm_lat.meta_value * PI( ) /180 ) ) “
. “* COS( ( (- 0.0802669 – pm_lng.meta_value) * PI( ) /180 )))) *180 / PI( )) *60 * 1.1515)) AS distance FROM “
. “wp_postmeta pm_lat INNER JOIN wp_postmeta pm_lng ON pm_lat.post_id = pm_lng.post_id AND “
. “pm_lng.meta_key = ‘lng’ WHERE pm_lat.meta_key = ‘lat’) d “
. “ON p.ID = d.post_id WHERE d.Distance <10000 and post_title LIKE ‘%searchphrase%’ ORDER BY d.Distance LIMIT 0 , 1000 ” ;
So its not the matter of adding filters and modifying the query by Join or providing custom where clause. Its the entire query. So i wrote a custom search results page with custom query and that works – problem solved but I can’t get my search form to submit to this custom page – different problem :).
My original search form tag “<form action=”<?php bloginfo(‘url’); ?>” method=”get” id=”searchform” class=”form_search”>”
My new search form tag below: Please note the “/dc” which is the custom page.
<form action=”<?php bloginfo(‘url’); ?>/dc” method=”get” id=”searchform” class=”form_search”>
but above strangely submits to http://www.sitename/dc/?querystrings instead of wwww.sitename/dc?querystrings. Note the “/” slash!
I hope this gives a rough idea of my problem?
There’s no reason the slash should make any difference in terms of reading with your querystring in the receiving page (in this case the dc page).
You can submit the page id or name in the search form if you want, then you can leave the form action pointing to the index..
<input type="hidden" name="page_id" value="ID_OF_PAGE_HERE" />
<input type="hidden" name="pagename" value="NAME_OF_PAGE_HERE" />
Thanks Mark. But it doesn’t work.
Even if i try directly put this URL in the browser address, wordpress still redirects me to the above slashed version.
for example: wwww.mysite.com/dc – works fine.
if use http://www.mysite.com/dc?s=search+phrase it redirects to http://www.mysite.com/dc/?s-search+phrase which results in 404! May be i’ve change something else in the wordpress url rewrite class for search functionality?
If the search finds matches it will display the correct page, but bear in mind the search by default looks at posts and the query at that point is for the current page, so you’ll not see anything but the page, even though you’ve requested a search and it’s found matches. You’ll see a 404 when the search didn’t find matches, the page when it does..
Are you attaching a page templae to this page?
Consider this, when you ask for a page, ie. example.com/dc, you’re actually just asking for .. example.com/?pagename=dc
Now when you append a search onto that URL you’re asking for a single page and a search (remember the search looks at posts). The search will naturally return a 404 page when it doesn’t find matches, that’s standard behaviour.
What i think you should be doing here is not using the WordPress search query var, but use your own, so WordPress doesn’t sling you over to a 404 page when the search doesn’t get a result..
If you do something like this..
Nothing will happen, you’ll remain on the /dc/ page, you can then do anything you like with the incoming query parameters without worrying about being redirected to a 404.
Hope that helps.. 🙂
Thanks Mark. My custom search page works if I call it directly. i.e wwww.sitename.com/search. yes, I’m attaching a page template. I’ve hard coded a mysql query for test purpose and the above link returns records and its fine on its own.
Problem is the when calling this page via a submit button / search form as described few replies back, Which redirects to http://www.sitename.com/search/?s=search+phrase&sa1=Search which redirects to the 404 page. So it drills down to the submit form instead of my custom search page as the custom page sitename.com/search works on its own.
I understand the below:
http://www.sitename.com/search = http://www.sitename.com?pagename=search. But i don’t think http://www.sitename.com/search?s=search+phrase = http://www.sitename.com/search/?s=search+phrase. The slash is the problem.
I want a way to let my search form call this custom query page with parameters.
Hope this narrows the problem? Any idea.. Much appreciate your response.
Mark – You’re right. Slash is not the problem. I found the problem!!! The reason I was not able to run my custom query page was the “s” query string parameter. Just to expand so other people can understand.
http://www.sitename.com/search/?s=search+phrase doesn’t work but try renaming your query string “s” = “somethingelse” (i.e sitename.com/search/?phrase=search+phrase) works beautifully. I’m not sure whether its due to the wordpress or my customised theme. I was just trying various things to get this working and just renaming the search text field name in search form made it work.
Thanks your patience Mark and if it wasn’t for your point on 404 behaviour, i wouldn’t have harded coded my custom query to isolate parts and eventually got where I’m now!
- The topic ‘[Resolved] Customising search query’ is closed to new replies.