Is there a way to use search parameters as conditional values?

Home Forums Product Support Forums Ajax Search Pro for WordPress Support Is there a way to use search parameters as conditional values?

This topic contains 9 replies, has 2 voices, and was last updated by

 
Keymaster
3 years, 3 months ago.

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #19349

    Participant

    Hello, 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 3 years, 4 months ago by  wikot.
    #19357

    Hi,

    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:

    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #19364

    Participant

    Hello, 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:

    if('field_name'=='value'){
    //HTML goes here}

    Thanks again for the help.

    #19375

    Well, 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

    // CONDITION 1 

    and the

    // CONDITION 1 END

    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.

    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #19394

    Participant

    Thank 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 help

    Attachments:
    You must be logged in to view attached files.
    #19397

    Hi,

    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:

    /* 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;
    }

    This might be far from a final good solution, but definitely a good way to start 🙂

    I hope this helps!

    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #19472

    Participant

    Hello, 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.

    #19473

    Hi!

    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.

    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #19512

    Participant

    Hello, 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:

     $query->set( 'orderby', array(
    			'meta_value_num'=>'ASC',
    			'title'=>'ASC'
    		) );
    		$query->set('meta_key', 'contenido');
    	    }
    #'contenido' is a boolean custom field

    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.

    • This reply was modified 3 years, 3 months ago by  wikot.
    • This reply was modified 3 years, 3 months ago by  wikot.
    • This reply was modified 3 years, 3 months ago by  wikot.
    Attachments:
    You must be logged in to view attached files.
    #19529

    Hi!

    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 🙂

    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


Viewing 10 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic.