Editing Global Site Search Form

I'm trying to edit the Global Site Search form to allow the search to be filtered. Just to see what would happen, I added

<select name = "order">
<option value="ASC">asc</option>
<option value="DESC">desc</option>
</select>

But no matter what happened to the URL, the order of the posts did not change. When it comes to editing the search form and modifying this plugin's query to filter results what am I missing?

  • Sajid

    Hello Justin,
    Hope you are doing good today

    Since this is not a default WordPress search form, so these fields would not effect the search results here.

    This is not possible to change the order of search results without customizing the code of the plugin itself.

    Please provide more details about what exactly you are after so I could flag one of our developer to get their invaluable feedback on this matter. If it could be done quickly, they will try to come up with a code based solution, otherwise you may need to hire a developer from jobs board.

    Also please note that, the developer response time might be slower than normal support staff here, so we appreciate your patience here.

    Take care and have a nice day

    Best Regards,
    Sajid - WPMU DEV Support

  • Justin

    Thanks for your reply. At least now I know that its going to take more than just modifying the form.

    Right now, I'm just trying to modify the query. Once I learn what functions I need to use to do that, then I should be able to modify it for specific results. For example, I would like to sort by marketpress product price or sort by some other meta value. Im just looking to get pointed in the right direction.

  • Sajid

    Hello Justin,
    Hope you are doing good today

    I am not really aware of code base of this plugin and does not have function name at hands. Let m please ping the SLS (code experts) here for their invaluable feedback on this matter.

    Please note that their response time might be slower than normal support staff response, so really appreciate your patience here.

    Take care and have a nice day

    Best Regards,
    Sajid - WPMU DEV Support

  • Mahlamusa

    Hello Justin,

    I hope you are doing great today.I am very sorry for the delayed response with regards to this issue. I have looked at the code of the Global Site Search plugin.

    I have tried to do it the way you attempted it and it works if you modify the form and then modify the output function to add the query parameters to the query that will be executed.

    You can do it like so: Add the filter HTML tags that will be used to filter the search results on the search form:

    <select name = "order">
                    	<option value="ASC">asc</option>
                    	<option value="DESC">desc</option>
    </select>
    <select name = "orderby">
                    	<option value="date">Date</option>
                    	<option value="name">Name</option>
    </select>

    Then in the file 'plugins/global-site-search/global-site-search.php' add the following lines towards the end of the function before the line 'ob_start()':

    function global_site_search_output( $content ){
     //add code before ob_start() at the end of this function
    }
    $parameters['order'] = isset( $wp_query->query_vars['order'] ) ? urldecode( $wp_query->query_vars['order'] ) : '';
    $parameters['orderby'] = isset( $wp_query->query_vars['orderby'] ) ? urldecode( $wp_query->query_vars['orderby'] ) : '';

    So you will finally have these lines at the end of the function:

    function global_site_search_output( $content ){
           //the rest of the code here,
            $parameters['order'] = isset( $wp_query->query_vars['order'] ) ? urldecode( $wp_query->query_vars['order'] ) : '';
    		$parameters['orderby'] = isset( $wp_query->query_vars['orderby'] ) ? urldecode( $wp_query->query_vars['orderby'] ) : '';
    		error_log( print_r ($parameters, true ) );
    		//=====================================//
    		ob_start();
    		$network_query_posts = network_query_posts( $parameters );
    		include global_site_search_locate_template( 'global-site-search.php' );
    		$content .= ob_get_clean();
    
    		return $content;
    }

    You should add all the filters that you added in the form. Please refer to this doc for the filters you can use: https://codex.wordpress.org/WordPress_Query_Vars

    This will work for you but you will have to modify the core of the plugin, as a result your code may be lost due to some plugin updates so keep in mind that you will have to apply these changes manually after being overwritten.

    Another method you can pursue to achieve the same is to explore the action hooks and filters of the plugin "Post Indexer" which provides the core functionality of the "Global Site Search" plugin. Finding the right hooks and filters will enable you to write code that will be separate from the search plugin and you will not have to worry about updates.

    I hope this helps, please let me know if I have missed something and if you need any more help.

    Enjoy the rest of your day.

    Cheers,
    Mahlamusa