0

Im using the wp_query with ajax to display and filter my custom post.

everything works fine until i try to use my extended version of wp_query in the args parameters.

my js file code:

jQuery(function($){
$('#filter').submit(function(){
    var filter = $('#filter');
    $.ajax({
        url:filter.attr('action'),
        data:filter.serialize(), // form data
        type:filter.attr('method'), // POST
        beforeSend:function(xhr){
            filter.find('button').text('Processing...'); // changing the button label
        },
        success:function(data){
            filter.find('button').text('Apply filter'); // changing the button label back
            $('#response').html(data); // insert data
        }
    });
    return false;
});});

the php part with args:

    $args = array(
    'post_type' => 'rides',
     'geo_query' => array( 
            'latfrom'      => '45.7579341',
            'lngfrom'      => '4.7650812', 
            'latto'      => '26.123555',
            'lngto'      => '6.5445445', 
            'distancefrom' => 10,
            'distanceto' => 10,
            'compare'  => '<='
            ),
);

the loop:

    $query = new WP_Query( $args );

if( $query->have_posts() ) :
    while( $query->have_posts() ): $query->the_post();
        echo '<h2>' . $query->post->post_title . '</h2>';
    endwhile;
    wp_reset_postdata();
else :
    echo 'No posts found';
endif;

die();
}

As you can see, I am using the custom geo_query parameter, which is an extension of the wp_query. This does work when using the standard wp_query BUT NOT when using it with ajax...

What am I doing wrong here ?

  • How is it not working? What is the response you are receiving? How are you calling your wp_query in the ajax call? – czerspalace Apr 13 '17 at 18:29
  • All the custom posts "rides" are displayed as a response...so filtering is not working. – john Apr 13 '17 at 19:00
  • How are you defining geo_query – czerspalace Apr 13 '17 at 19:08
  • Can you add more of your server code that is called from AJAX? – czerspalace Apr 14 '17 at 0:27
  • I have added all my code below and above.. really i have tried everything.. can't find the issue. The geo_query extension works fine when using the classic wp_query method.. but not when using ajax :( – john Apr 14 '17 at 10:38
0

this is my geo_query extension of wp_query:

add_filter( 'posts_fields', 'willy_geo_fields', 10, 2 );
function willy_geo_fields( $fields, $q ) {
if ( isset( 
      $q->query_vars['geo_query']['latfrom'],
      $q->query_vars['geo_query']['lngfrom'],
      $q->query_vars['geo_query']['latto'],
      $q->query_vars['geo_query']['lngto'],
      $q->query_vars['geo_query']['distancefrom'],
      $q->query_vars['geo_query']['distanceto']

      ) ) {

    global $wpdb;
    $fields .= $wpdb->prepare(", ( 6371 * acos( cos( radians( %f ) ) 
                    * cos( radians( metalatfrom.meta_value ) ) 
                    * cos( radians( metalngfrom.meta_value ) -     radians( %f ) ) 
                    + sin( radians( %f ) ) 
                    * sin( radians( metalatfrom.meta_value ) ) ) ) AS distancefrom
                    , ( 6371 * acos( cos( radians( %f ) ) 
                    * cos( radians( metalatto.meta_value ) ) 
                    * cos( radians( metalngto.meta_value ) - radians( %f ) ) 
                    + sin( radians( %f ) ) 
                    * sin( radians( metalatto.meta_value ) ) ) ) AS distanceto
    ",
                  $q->query_vars['geo_query']['latfrom'],
                  $q->query_vars['geo_query']['lngfrom'],
                  $q->query_vars['geo_query']['latfrom'],
                  $q->query_vars['geo_query']['latto'],
                  $q->query_vars['geo_query']['lngto'],
                  $q->query_vars['geo_query']['latto'] );
}
return $fields;
}
  add_filter( 'posts_join', 'willy_geo_join', 10, 2 );
  function willy_geo_join( $join, $q) {
if ( isset( 
      $q->query_vars['geo_query']['latfrom'],
      $q->query_vars['geo_query']['lngfrom'],
      $q->query_vars['geo_query']['distancefrom'],
      $q->query_vars['geo_query']['lngto'],
      $q->query_vars['geo_query']['lngto'],
      $q->query_vars['geo_query']['distanceto'] ) ) {
    global $wpdb;
    $join .= " INNER JOIN $wpdb->postmeta AS metalatfrom ON ( $wpdb->posts.ID = metalatfrom.post_id )";
    $join .= " INNER JOIN $wpdb->postmeta AS metalngfrom ON ( $wpdb->posts.ID = metalngfrom.post_id )";
    $join .= " INNER JOIN $wpdb->postmeta AS metalatto ON ( $wpdb->posts.ID = metalatto.post_id )";
    $join .= " INNER JOIN $wpdb->postmeta AS metalngto ON ( $wpdb->posts.ID = metalngto.post_id )";
 }
 return $join;
 }
    add_filter( 'posts_where', 'willy_geo_where', 10, 2 );
   function willy_geo_where( $where, $q ) {
    if ( isset( 
      $q->query_vars['geo_query']['latfrom'],
      $q->query_vars['geo_query']['lngfrom'],
      $q->query_vars['geo_query']['distancefrom'],
      $q->query_vars['geo_query']['latto'],
      $q->query_vars['geo_query']['lngto'],
      $q->query_vars['geo_query']['distanceto'] ) ) {

    global $wpdb;
    $where .= " AND metalatfrom.meta_key = 'latfrom'";
    $where .= " AND metalngfrom.meta_key = 'lngfrom'";
    $where .= " AND metalatto.meta_key = 'latto'";
    $where .= " AND metalngto.meta_key = 'lngto'";
 }
 return $where;
 }
add_filter( 'posts_groupby', 'willy_geo_distance', 10, 2 );
function willy_geo_distance( $groupby, $q ) {
if ( isset( 
     $q->query_vars['geo_query']['latfrom'],
      $q->query_vars['geo_query']['lngfrom'],
      $q->query_vars['geo_query']['distancefrom'],
      $q->query_vars['geo_query']['latto'],
      $q->query_vars['geo_query']['lngto'],
      $q->query_vars['geo_query']['distanceto'] ) ) {

    $compare = '<=';
    if ( isset( $q->query_vars['geo_query']['compare'] )
      && in_array( $q->query_vars['geo_query']['compare'], array( '<', '<=', '>', '>=' ) ) ) {
        $compare = $q->query_vars['geo_query']['compare'];
    }
    global $wpdb;
    $groupby .= $wpdb->prepare( "$wpdb->posts.ID HAVING distancefrom <= %d AND distanceto <= %d ",
                  $q->query_vars['geo_query']['distancefrom'],
                  $q->query_vars['geo_query']['distanceto'] );
}
return false;
 }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.