WordPress一起运行两个查询

时间:2014-05-15 11:39:22

标签: php mysql wordpress geolocation

这是一个事件网站,用于处理每个事件相对于用户位置的位置,并在搜索结果中显示距离。

我使用WP GeoQuery扩展程序允许我根据距离进行查询。但是,它对tax_query之类的东西不起作用。所以,我想知道是否有运行WP_GeoQuery来查询特定距离内的事件的方法,然后通过正常的Wp_Query运行结果以获得它过滤正确的分类法和其他论点?

以下是地理查询:

$url = "http://freegeoip.net/json/". $_SERVER['REMOTE_ADDR'] .'';
$geo = json_decode(file_get_contents($url), true);

$geo_query = new WP_GeoQuery(array(
    // location stuff
    'latitude' => $geo[latitude], // User's Latitude (optional)
    'longitude' => $geo[longitude], // User's Longitude (optional)

    // radius breaks the query if using tax_query too
    'radius' => 25 // Radius to select for in miles (optional)
));

这是正常的WP_Query:

// add query for title
        function title_filter( $where, &$wp_query ) {
            global $wpdb;
            if ( $search_term = $wp_query->get( 'title_like' ) ) {
                $where .= ' AND ' . $wpdb->posts . '.post_title LIKE \'%' . esc_sql( like_escape( $search_term ) ) . '%\'';
            }
            return $where;
        }
        add_filter( 'posts_where', 'title_filter', 10, 2 );



        // dates to and from
        function date_from( $from ) {
            $from = str_replace("meta_key = 'date_%_start-date'", "meta_key LIKE 'date_%_start-date'", $from);
            return $from;
        }
        add_filter('posts_where', 'date_from');

        function date_to( $to ) {
            $to = str_replace("mt1.meta_key = 'date_%_end-date'", "mt1.meta_key LIKE 'date_%_end-date'", $to);
            return $to;
        }
        add_filter('posts_where', 'date_to');





        // convert date to yyyymmdd
        $date1 = str_replace('/', '-', $_POST['when']);
        $when = date("Ymd", strtotime($date1));

        $date2 = str_replace('/', '-', $_POST['when-2']);
        $when2 = date("Ymd", strtotime($date2));

    ?>

    <h1>Search</h1>

    <div class="events">

        <?php $args = array(  
            // general
            'post_type' => 'event',
            'post_status' => 'publish',
            'posts_per_page' => 2,
            'paged' => $paged,

            // what input
            'title_like' => $_POST['what'],

            // category filter
            'tax_query' => array(
                array(
                    'taxonomy' => 'main-cat',
                    'field' => 'slug',
                    'terms' => $_POST['main-cat']
                )
            ),

            // date filter
            'meta_query' => array(
                'relation' => 'AND',
                array(
                    'key' => 'date_%_start-date',
                    'value' => $when,
                    'compare' => '>=',
                    'type' => 'DATE'
                ),
                array (
                    'key' => 'date_%_end-date',
                    'value' => $when2,
                    'compare' => '<=',
                    'type' => 'DATE'
                )
            ),


        );

        $temp = $wp_query;
        $wp_query = null;
        $wp_query = new WP_Query( $args );
        $wp_query->query('posts_per_page=2&post_type=event'.'&paged='.$paged);

单独使用时,它们可以正常工作。但我不确定如何运行其中一个,然后使用结果来完成另一个查询。这甚至可能吗?

0 个答案:

没有答案