Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › Is there a way to use search parameters as conditional values?
- This topic has 9 replies, 2 voices, and was last updated 7 years, 7 months ago by
Ernest Marcinko.
-
AuthorPosts
-
September 27, 2018 at 4:46 pm #19349
wikot
ParticipantHello, I’m wondering if there’s a way to use the search parameters as a conditional value. I want to display certain information in the results if certain parameters match. For example, I have a search parameter that is a custom field and is called ‘city’ and another called ‘zone’. I want to display an ad if city equals ‘A’ and zone equals ‘1’ and another if city equals ‘B’ and zone equals ‘2’ and so on. Is this possible?
I hope I made myself clear, thanks for any help.
-
This topic was modified 7 years, 8 months ago by
wikot.
September 28, 2018 at 2:06 pm #19357Ernest Marcinko
KeymasterHi,
Well, there are a few hooks available, depending on how exactly you want to realize this. If you open wp-content\plugins\ajax-search-pro\includes\views\asp.shortcode.results.php you can see all of them. There are two that stand out, in case you want to place custom HTML there: asp_layout_before_results and asp_layout_before_first_result
For example, here is a custom function to parse the passed options to the $options variable. For example, test_field value should be in $options[‘aspf’][‘test_field_1’] variable.
You can echo the custom HTML you need within that function:September 28, 2018 at 10:54 pm #19364wikot
ParticipantHello, thanks for your answer.
I think I’m very close but when I try your code and try a var_dump it returns empty, is there a step I’m missing?
I don’t understand where is the search_id declared, is it inside the function? or is it given as a parameter? I’m a begginer with PHP, sorry.
Other thing I saw when trying your code is that the HTML code I try to echo with the hooks, gets to the bottom of the page, after the visible area.
What I want is to be able to do is to use an if statement like this:
[code]if(‘field_name’==’value’){
//HTML goes here}[/code]
Thanks again for the help.October 1, 2018 at 2:48 pm #19375Ernest Marcinko
KeymasterWell, I had to do some testing to make a semi-working solution, and there might be a way to do it. This is not an easy task, because there is no simple syntax to compare values in a manner such as $field == ‘value’, because the plugin supports multi-instance/multi print layouts, that complicate this a lot.
Nevertheless, I have made the simplest possible solution, which produces a s text printed basically within the first result (but separately), which can be replaced with anything of course. It produces something like this: https://i.imgur.com/6pR6vB4.png
And the code is this:
The important bit is between the
[html]// CONDITION 1 [/html]
and the
[html]// CONDITION 1 END[/html]
lines (22-36).Basically you can change the $field_name and the $value variables. Then the code will compare the custom field defined in $field_name with the value defined in $value variables.
Then, between lines 31-33 a custom HTML is printed, that is visible on the screenshot as well. I recommend leaving lines 31 and 33 with the DIV element as they are, as it contain important styling to increase the z-index above the overlay of the first item.You can also make copies of the code between CONDITION bit to make more different conditions with different outputs.
I am not sure how much this helps, but this is the simplest way I was able to get it working.
October 3, 2018 at 11:59 pm #19394wikot
ParticipantThank you so much for the help, I ended up using jQuery to solve this since it was easier, but couldn’t have done it without your help.
Now, I was wondering if there’s a way to move the magnifying glass to the end of the search boxes, like the attached image.
Thanks again for the helpOctober 4, 2018 at 11:20 am #19397Ernest Marcinko
KeymasterHi,
Thank you, I am glad I was able to help you.
Well, there is a search button, that can be used as a filter, but that does not contain a magnifier icon. However, with some custom CSS + configuration, it might just be possible to change it to a magnifier.
This is what I was able to achieve: https://i.imgur.com/ifGeok2.png
And this is what I did:
1. I used this configuration for the search button: https://i.imgur.com/gwab9xx.png
2. + used this custom CSS to change the background to a magnifier & to hide the original magnifier:[html]/* Changing the search button to a magnifier */
button.asp_search_btn.asp_s_btn {
background-image: url(‘https://upload.wikimedia.org/wikipedia/commons/thumb/a/a0/Antu_document-edit-verify.svg/512px-Antu_document-edit-verify.svg.png’) !important;
background-size: 20px !important;
background-position: center !important;
background-repeat: no-repeat !important;
}/* To hide the original magnifier */
.promagnifier {
display: none !important;
}[/html]This might be far from a final good solution, but definitely a good way to start 🙂
I hope this helps!
October 9, 2018 at 7:10 pm #19472wikot
ParticipantHello, thanks so much for your answer, it works well
Sorry for being a pain. but I have another question, is there a way to sort the results alphabetically?
Thanks again for the help.October 9, 2018 at 7:43 pm #19473Ernest Marcinko
KeymasterHi!
You are welcome!
Well, there is no front-end filter (yet) for that, but you can change the fixed primary and secondary order on the back-end, under the General Options -> Ordering panel.
October 18, 2018 at 1:12 am #19512wikot
ParticipantHello, thanks so much for the answers, you’ve been of great help.
Sorry to bother you again, but I was wondering if there’s a way to order the search results using this custom query:
[php] $query->set( ‘orderby’, array(
‘meta_value_num’=>’ASC’,
‘title’=>’ASC’
) );
$query->set(‘meta_key’, ‘contenido’);
}
#’contenido’ is a boolean custom field[/php]
I’ve tried to use it with the pre_get_posts hook combined the is_search() conditional but it doesn’t seem to work:
Thanks again for the help.
Edit: I added an image of the query because the code isn’t being displayed correctly.October 18, 2018 at 1:07 pm #19529Ernest Marcinko
KeymasterHi!
This is not going to work in this case, as the search query is different from the default search query.
However, on the back-end, you can define an ordering by custom fields: https://i.imgur.com/HAJLJbK.png
That should do the trick 🙂 -
This topic was modified 7 years, 8 months ago by
-
AuthorPosts
- You must be logged in to reply to this topic.