Wordpress $wp_query with CPT Distance and Featured Orderby


Wordpress $wp_query with CPT Distance and Featured Orderby

首次在此处发布;在过去的几天里,我一直在做这件事,我很接近,但被卡住了。使用一个支持不足的主题,该主题与我们所有的定制都能很好地配合使用。这是一个商业目录网站。主题包括"特色"位置,以更改格式并突出显示输出的列表。该主题还为列表使用了自定义的帖子类型。用户输入一个城市、州,主题会返回尽可能多的搜索半径内的列表,我们正在努力让在该半径内找到的任何特色列表都能排在结果的首位,即使它不是"最接近的"。主题使用以下代码设置geo.php文件中的"距离":

static function posts_clauses( $clauses, $wp_query ) {
    extract(appthemes_geo_get_args());
    global $wpdb;
    $geo_query = $wp_query->get( 'app_geo_query' );
    if ( !$geo_query )
        return $clauses;
    extract( $geo_query, EXTR_SKIP );
    $R = 'mi' == $unit ? 3959 : 6371;
    $clauses['join'] .= $wpdb->prepare( " INNER JOIN (
        SELECT post_id, ( %d * acos( cos( radians(%f) ) * cos( radians(lat) ) * cos( radians(lng) - radians(%f) ) + sin( radians(%f) ) * sin( radians(lat) ) ) ) AS distance FROM $wpdb->app_geodata
    ) as distances ON ($wpdb->posts.ID = distances.post_id)
    ", $R, $lat, $lng, $lat );
    $clauses['where'] .= $wpdb->prepare( " AND distance < %f", (float) $rad );
    if ( 'distance' == $wp_query->get( 'orderby' ) ) {
        $clauses['orderby'] = 'distance ' . ( 'DESC' == strtoupper( $wp_query->get( 'order' ) ) ? 'DESC' : 'ASC' );
    }
    return $clauses;
}

}

然后在另一个文件(views.php)中,有一些参数可以按最高评级、字母顺序、最新等更改排序顺序。我们遇到的困难是试图让特色列表出现在距离结果之上。有一个wp_query使用以下代码返回顶部的特性:

 function parse_query( $wp_query ) {
    global $va_options, $wpdb;
    $wp_query->set( 'ls', trim( get_query_var( 'ls' ) ) );
    $wp_query->set( 's', get_query_var( 'ls' ) );
    $wp_query->set( 'post_type', VA_LISTING_PTYPE );
    $wp_query->set( 'posts_per_page', $va_options->listings_per_page );
    if ( '' == $wp_query->get( 'order' ) )
        $wp_query->set( 'order', 'asc' );
    $orderby = $wp_query->get( 'orderby' );
    if ( empty( $orderby ) ) {
        $location = trim( $wp_query->get( 'location' ) );
        if ( !empty( $location ) ) {
            $orderby = $va_options->default_geo_search_sort;
        } else {
            $orderby = $va_options->default_search_sort;
        }
        $wp_query->set( 'orderby', $orderby );
    }
    $wp_query->set( 'va_orderby', $orderby );
    switch ( $orderby ) {
 case 'default':
        default:
            $wp_query->set( 'meta_key', VA_ITEM_FEATURED );
            $wp_query->set( 'orderby', 'meta_value_num' );
            $wp_query->set( 'order', 'desc' );
            $wp_query->set( 'va-featured', true );
            break;
    }

我删除了其他一些"案例",使代码更易于遵循。默认情况下,切换顺序中的距离如下所示:

            case 'distance':
            break;

这显然是因为orderby是在geo.php文件中定义的。我尝试添加$wp_query->set('meta_key','featured_cat');(它是一个1或0的数字,其中1是特色),然后为orderby添加一个数组,但它不接受。

我想答案就在眼前,但我希望得到一些帮助。如有任何协助,我们将不胜感激。

想明白了。我注释掉了geo.php文件中的Orderby子句

if ( 'distance' == $wp_query->get( 'orderby' ) ) {
    $clauses['orderby'] = 'distance ' . ( 'DESC' == strtoupper( $wp_query->get( 'order' ) ) ? 'DESC' : 'ASC' );
}

并将orderby规则移动到views.php中。使用Wordpress 4.0可以对orderby进行细粒度控制。