• I’ve implemented this plugin strictly for a single custom post type, and have half successfully customized the AJAX search result by modifying the uwpqsf-process-class.php. Probably not the best way to do it, but as I say, it’s half working for me.

    The trouble I’m having is passing arguments to the $arg variable in the uajax_result function where I’ve made the changes required for the output I need. This is just a simple example of the arguments I’ve tried to pass, but also lets you see what I’ve done with the function. Passing even the simple function of posts_per_page has done nothing at all.

    function uajax_result($arg, $id,$pagenumber,$getdata){
    
        	$query = new WP_Query( $arg );
    		$html ='';
    
    		$arg = array(
    							'posts_per_page' => 3,
    		);
    
    		//print_r($query);	// The Loop
    
    	if ( $query->have_posts() ) {
    
    	 echo '<h2>Search results<span style="float:right;"><a href="" class="arconix-button arconix-button-small arconix-button-red" style="font-size:12px;">Reset</a></h2><table id="uniprofile"><tr class="head"><td>Class</td><td>University</td><td>Country</td><td>Acceptances</td><td>Offers</td><td>Matriculations</td>';
    
    	  while ( $query->have_posts() ) {
    	   $query->the_post();global $post; ?>
    
                <tr>
                <td><?php meta('uni-class'); ?></td>
    			<td><a href="<?php get_permalink(); ?>" rel="bookmark"><?php meta('uni-name'); ?></a></td>
                <td><?php meta('uni-country'); ?></td>
                                <td class="stat"><?php meta('uni-acceptances'); ?></td>
                                <td class="stat"><?php meta('uni-offers'); ?></td>
                                <td class="stat"><?php meta('uni-matriculations'); ?></td></tr>
    
    		 <?php }
    			$html .= $this->ajax_pagination($pagenumber,$query->max_num_pages, 4, $id,$getdata);
    		 } else {
    					$html .= __( 'Nothing Found.<br /><a href="" class="arconix-button arconix-button-small arconix-button-red" style="font-size:12px;">Reset</a>', 'UWPQSF' );
    				}
    				/* Restore original Post Data */
    
    				wp_reset_postdata();
    
    		return $html;
    
      }//end result

    Since making these customizations, I found your reference for the hooks on this page (http://9-sec.com/2014/01/ultimate-wp-query-search-filter/), which seems like it should be the correct way to make any customizations, but to be honest I am not sure how to implement the hook.

    If I can back-track, I have a custom template which applies the following nested loop:

    <?php
    
    $all = get_posts(array(
    'post_type'=> 'university',
    'posts_per_page' => -1)
    );
    
    $parents = array();
    
    foreach ($all as $single)
    
    {
    $kids = get_children($single->ID); 
    
    if(isset($kids) && !empty($kids) && count($kids) >= 1)
    {
    $parents[] = $single->ID;
    }
    
    }
    $args = array(
    'post_type' => 'university',
    'posts_per_page' => -1,
    'post__not_in' => $parents,
    'meta_key' => 'uni-class',
    'meta_query'   => array(
    	array(
    	'key' => 'uni-class'
    	),
    	array(
    	'key' => 'uni-country'
    	),
    	array(
    	'key' => 'uni-name'
    	)
    	)
    	);
    
    add_filter('posts_orderby','customorderby');
    
    $uniplace = new WP_Query($args);
    while ($uniplace->have_posts() ) : $uniplace->the_post(); ?>
    
    <tr>
    <td><?php meta('uni-class'); ?></td>
    <td><a href="<?php the_permalink(); ?>"><?php meta('uni-name'); ?></a></td>
    <td><?php meta('uni-country'); ?></td>
    <td class="stat"><?php meta('uni-acceptances'); ?></td>
    <td class="stat"><?php meta('uni-offers'); ?></td>
    <td class="stat"><?php meta('uni-matriculations'); ?></td>
    </tr>
    
    <?php remove_filter('posts_orderby','customorderby'); ?>
    
    <?php	endwhile;
    wp_reset_postdata(); ?>
    
    </table>

    The filter which is applied to this loop
    add_filter('posts_orderby','customorderby'); … and then removed remove_filter('posts_orderby','customorderby');

    allows me to sort by the multiple meta_values (MV1, then MV2, then MV3) I have in place. The function is as follows:

    function customorderby($orderby) {
        return 'mt1.meta_value DESC, mt2.meta_value ASC, mt3.meta_value ASC';
    }

    And so, my ultimate goal is to apply the same ordering filter on the AJAX results I’m achieving through the first block of code I posted above.

    Apologies if there is any lack of clarity, but I’ve tried to paint the full picture of what I’ve done. The page and site for which I have this implemented this one is currently only hosted locally.

    https://wordpress.org/plugins/ultimate-wp-query-search-filter/

Viewing 8 replies - 1 through 8 (of 8 total)
  • Thread Starter trevolutionary

    (@jkdsouljah)

    Update: I dug a bit deeper and applied the hook correctly rather than messing with the uwpqsf-process-class.php file. I’m still unable to pass custom parameters to the query, however if there was a way I could apply the above customorderby function to the results created by the hook, that’s the main thing I’m looking to achieve.

    Many thanks

    Thread Starter trevolutionary

    (@jkdsouljah)

    Here’s a live sample version of how I’ve implemented this.
    http://www.trevelations.com/dev/university-acceptance-profile/

    If you try the search filter with no criteria selected you’ll see that all the results are returned as expected by they are out of order.

    Thread Starter trevolutionary

    (@jkdsouljah)

    Okay, sorry for this. I failed to see earlier the “Result Setting and Others” section of the admin for the plugin, and now I can sort the result by one meta_value/meta_key (in this example, uni-class).

    I still would like to be able to apply the customorderby function above, or else find an alternative way to search the results by firstly, uni-class (desc), then uni-country (asc) and finally uni-name (asc), as I’ve been able to do in the custom template for this page prior to any filtering.

    Many thanks

    Thread Starter trevolutionary

    (@jkdsouljah)

    I’ll quit being a pain in the butt soon.

    I’ve now found how I can inject custom arguments into the loop thanks to one of your previous posts, and right now the relevant functions within my functions.php are as follows:

    function customorderby($orderby) {
        return 'mt1.meta_value DESC, mt2.meta_value ASC, mt3.meta_value ASC';
    }
    
    /************
    CUSTOMIZE AJAX RESULT OF ULTIMATE WORDPRESS QUERY SEARCH FILTER
    ************/ 
    
    add_filter('uwpqsf_result_tempt', 'customize_output', '', 4);
    function customize_output($results , $arg, $id, $getdata ){
    	 // The Query
                $apiclass = new uwpqsfprocess();
    			$query = new WP_Query( $arg );
    
    			$result = array (
    				'posts_per_page' => 3
    			);
    
    			ob_start();	$result = '';
    
    		// The Loop
    
    			if ( $query->have_posts() ) {
    			echo '<h2>Search results
    					<span style="float:right;">
    						<a href="" class="arconix-button arconix-button-small arconix-button-red" style="font-size:12px;">Reset</a>
    					</span>
    					</h2>
    
    			<table id="uniprofile">
    				<tr class="head">
    					<td>Class</td>
    					<td>University</td>
    					<td>Country</td>
    					<td>Acceptances</td>
    					<td>Offers</td>
    					<td>Matriculations</td>';
    
    			while ( $query->have_posts() ) {
    				$query->the_post(); ?>
    
    			<tr>
                    <td><?php meta('uni-class'); ?></td>
                    <td><a href="<?php get_permalink(); ?>" rel="bookmark"><?php meta('uni-name'); ?></a></td>
                    <td><?php meta('uni-country'); ?></td>
                    <td class="stat"><?php meta('uni-acceptances'); ?></td>
                    <td class="stat"><?php meta('uni-offers'); ?></td>
                    <td class="stat"><?php meta('uni-matriculations'); ?></td>
                </tr>
    
    			<?php }
                            echo  $apiclass->ajax_pagination($arg['paged'],$query->max_num_pages, 4, $id, $getdata);
    
    					} else {
    
    						echo  'no post found';
    				}
    				/* Restore original Post Data */
    				wp_reset_postdata();
    
    		$results = ob_get_clean();
    			return $results;
    }
    
    add_filter('uwpqsf_query_args','injecting_custom_arg','',4);
    function injecting_custom_arg($args, $id,$getdata){
      //$getdata is the data from the form input. Using it like GET url parameter
     $args['meta_key'] = 'uni-class';
     $args['posts_per_page'] = -1;
     $args['meta_query'] = array(array(array('key' => 'uni-class'), array('key' => 'uni-country'), array('key' => 'uni-name')));
    
     return $args;
    }

    So now I only need to figure out how to apply the customorderby function or sort the results by those 3 respective meta_keys.

    Thanks a lot

    Plugin Author TC.K

    (@wp_dummy)

    There are two ways for you to customize the query. First is by uwpqsf_query_args() filter, another is through the result template filter uwpqsf_result_tempt().

    The problem of your customize_output function above is that you inject the $arg after the WP_Query class, which is not correct. You should pass the $arg to the WP_query class after you have customized your $arg variable.

    Also, I have no idea where does this come from.

    function customorderby($orderby) {
        return 'mt1.meta_value DESC, mt2.meta_value ASC, mt3.meta_value ASC';
    }

    Thread Starter trevolutionary

    (@jkdsouljah)

    I successfully customized the query using this function in my functions.php file:

    add_filter('uwpqsf_query_args','injecting_custom_arg','',4);
    function injecting_custom_arg($args, $id,$getdata){
      //$getdata is the data from the form input. Using it like GET url parameter
     $args['meta_key'] = 'uni-class';
     $args['posts_per_page'] = -1;
     $args['meta_query'] = array(array(array('key' => 'uni-class'), array('key' => 'uni-country'), array('key' => 'uni-name')));
    
     return $args;
    }

    This function is a completely separate function to your plugin/the ajax search results:

    function customorderby($orderby) {
        return 'mt1.meta_value DESC, mt2.meta_value ASC, mt3.meta_value ASC';
    }

    It allows me to sort my nested query by multiple meta_keys (uni-class, then uni-country, then uni-name) on my page template as follows (prior to filtering the results with your plugin). The relevant lines of code to apply this filter are:

    add_filter('posts_orderby','customorderby');
    [Nested Query]
    remove_filter('posts_orderby','customorderby');

    In application, it looks like this in my custom page template:

    <?php
    
    						/*$all = get_posts(array(
    							'post_type'=> 'university',
    							'posts_per_page' => -1)
    						);
    
    						$parents = array();
    
    						foreach ($all as $single)
    
    						{
    							$kids = get_children($single->ID);  
    
    							if(isset($kids) && !empty($kids) && count($kids) >= 1)
    
    							{
    								$parents[] = $single->ID;
    							}
    						}*/
    
    						$args = array(
    							'post_type' => 'university',
    							'posts_per_page' => -1,
    							/*'post__not_in' => $parents,*/
    							'meta_key' => 'uni-class',
    							'meta_query'   => array(
    								array(
    									'key' => 'uni-class'
    								),
    								array(
    									'key' => 'uni-country'
    								),
    								array(
    									'key' => 'uni-name'
    								)
    							)
    						);
    
    						add_filter('posts_orderby','customorderby');
    						$uniplace = new WP_Query($args);
    						while ($uniplace->have_posts() ) : $uniplace->the_post(); ?>
    						<tr>
    							<td><?php meta('uni-class'); ?></td>
                                <td><a href="<?php the_permalink(); ?>"><?php meta('uni-name'); ?></a></td>
                                <td><?php meta('uni-country'); ?></td>
                                <td class="stat"><?php meta('uni-acceptances'); ?></td>
                                <td class="stat"><?php meta('uni-offers'); ?></td>
                                <td class="stat"><?php meta('uni-matriculations'); ?></td>
                            </tr>
                          <?php remove_filter('posts_orderby','customorderby'); ?>
    
    					<?php	endwhile;
    						wp_reset_postdata();
    					?>
                        </table>

    If you take a look at http://www.trevelations.com/dev/university-acceptance-profile/, without filtering any data, you’ll see that the data is ordered firstly in descending order in the Class column, then within that, ascending order is the Country column, and finally by ascending order in the University column. That’s what the customorderby function and filter achieve (before filtering).

    For clarity, everything is working now except the ability to order the search results by multiple meta_keys – I can only order by a single one meta_key.

    Plugin Author TC.K

    (@wp_dummy)

    If you want to add ordering selection in the search form, you can refer to this thread.

    Thread Starter trevolutionary

    (@jkdsouljah)

    Yes, that’s where I found the injecting_custom_arg function that I put into my functions.php. It works, but only for a single meta_key. Guess I’ve got some experimenting to do.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Customizing AJAX result’ is closed to new replies.