在wordpress Ajax函数中加入2个搜索查询

时间:2017-01-31 12:41:46

标签: wordpress search join woocommerce

您好我已尝试通过sku搜索加入常见搜索的结果,但似乎没有任何效果,它将显示常见搜索或sku,而不是两者,任何帮助都将被赞赏

if (!function_exists('yolo_result_search_callback')) {
function yolo_result_search_callback() {
    ob_start();

    $yolo_options = yolo_get_options();
    $posts_per_page = 8;

    if (isset($yolo_options['search_box_result_amount']) && !empty($yolo_options['search_box_result_amount'])) {
        $posts_per_page = $yolo_options['search_box_result_amount'];
    }

    $post_type = array();
    if (isset($yolo_options['search_box_post_type']) && is_array($yolo_options['search_box_post_type'])) {
        foreach($yolo_options['search_box_post_type'] as $key => $value) {
            if ($value == 1) {
                $post_type[] = $key;
            }
        }
    }

    $keyword = $_REQUEST['keyword'];

    if ( $keyword ) {


    $search_query = array(
    'post_type' => 'product',
    'meta_query' => array(
        array(
            'key' => '_sku',
            'value' => $keyword,
            'posts_per_page' => $posts_per_page + 1,
            'compare' => 'LIKE',
            'relation' => 'OR',
            )
        )
        );


        $search_query = array(
            's'              => $keyword,
            'order'          => 'DESC',
            'orderby'        => 'date',
            'post_status'    => 'publish',
            'post_type'      => $post_type,
            'posts_per_page' => $posts_per_page + 1,
        );

        $search = new WP_Query($search_query);

        $newdata = array();
        if ($search && count($search->post) > 0) {
            $count = 0;
            foreach ( $search->posts as $post ) {
                if ($count == $posts_per_page) {
                    $newdata[] = array(
                        'id'        => -2,
                        'title'     => '<a href="' . site_url() .'?s=' . $keyword . '"><i class="wicon icon-outline-vector-icons-pack-94"></i> ' . esc_html__('View More','yolo-motor') . '</a>',
                        'guid'      => '',
                        'date'      => null,
                    );

                    break;
                }
                $newdata[] = array(
                    'id'        => $post->ID,
                    'title'     => $post->post_title,
                    'guid'      => get_permalink( $post->ID ),
                    'date'      => mysql2date( 'M d Y', $post->post_date ),
                );
                $count++;

            }
        }
        else {
            $newdata[] = array(
                'id'        => -1,
                'title'     => esc_html__( 'Sorry, but nothing matched your search criteria. Please try again with some different keywords.', 'yolo-motor' ),
                'guid'      => '',
                'date'      => null,
            );
        }

        ob_end_clean();
        echo json_encode( $newdata );
    }
    die(); // this is required to return a proper result
}
add_action( 'wp_ajax_nopriv_result_search', 'yolo_result_search_callback' );
add_action( 'wp_ajax_result_search', 'yolo_result_search_callback' );

 }

1 个答案:

答案 0 :(得分:0)

在玩了一整天后,我想出了解决方案,也许它可以帮助别人

DatabaseReference ref = mFirebaseAdapter
getRef(indexOfDeletedItem);

}