Customizing AJAX result
-
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 removedremove_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/
- The topic ‘Customizing AJAX result’ is closed to new replies.