【问题标题】:Wordpress $wp_query with CPT Distance and Featured Orderby带有 CPT 距离和精选排序的 Wordpress $wp_query
【发布时间】:2014-10-11 00:39:16
【问题描述】:

第一次在这里发帖;过去几天一直在这上面,我很接近但卡住了。使用支持不佳的主题,该主题非常适合我们所有的自定义。这是一个商业目录网站。主题包括“精选”位置以更改格式并突出显示输出的列表。该主题还使用列表的自定义帖子类型。用户输入一个城市、州和主题返回尽可能多的列表在搜索范围内,我们试图让在该范围内找到的任何特色列表出现在结果的顶部,即使它不是“最近的”。主题使用以下代码在 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;
    }

我删除了一些其他“案例”,以使代码更易于理解。 switch orderby 中的默认距离如下所示:

            case 'distance':
            break;

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

我认为答案正盯着我看,但我希望能得到一些帮助。感谢您提供任何帮助。

【问题讨论】:

    标签: php custom-post-type wordpress


    【解决方案1】:

    想通了。我注释掉了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,可以对 orderbys 进行精细控制。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-08-29
      • 1970-01-01
      • 1970-01-01
      • 2018-06-24
      • 1970-01-01
      • 2020-03-18
      • 2018-08-07
      相关资源
      最近更新 更多