WP_Query extended with AJAX

时间:2017-04-13 作者:john

Im使用带有ajax的wp\\U查询来显示和过滤我的自定义帖子。

在我尝试在args参数中使用扩展版本的wp\\u query之前,一切都正常。

我的js文件代码:

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;
});});
带有参数的php部分:

    $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\'  => \'<=\'
            ),
);
循环:

    $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();
}
如您所见,我使用的是自定义geo\\u查询参数,它是wp\\u查询的扩展。这在使用标准wp\\U查询时有效,但在将其与ajax一起使用时无效。。。

我做错了什么?

1 个回复
SO网友:john

这是wp\\u query的my geo\\u 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;
 }