使用POST_WHERE查询包括自定义元数据

时间:2012-05-05 作者:Ralphonz

我一直在四处寻找如何做到这一点,但我找到的解决方案似乎对我不起作用。

我正在使用AJAX搜索一些wordpress自定义帖子类型。我已经走得很远了,一切都很顺利。除了现在,我一直在研究如何在搜索中包含自定义帖子类型的自定义字段(以及内容和标题)。我只想添加到SQL语句中进行搜索,但我对SQL不是很精通。

我试过了

以下是我到目前为止的函数。php在AJAX调用发送搜索词后查询数据库:

function display_all_products() {

        global $term;
        $term = trim(strip_tags($_POST[\'s\']));
            if(!empty($_POST[\'s\']))
            {
                add_filter( \'posts_where\' , \'product_posts_where\' );
            }
            $args = array( 
                \'posts_per_page\' => 50,
                \'paged\' => 1, 
                \'post_type\' => \'product\', 
                \'order\' => \'DESC\',
            );

            $the_query = new WP_Query( $args ); 

            if($the_query->have_posts()) {
                while ( $the_query->have_posts() ) {
                    $the_query->the_post();

                    $pid = get_the_ID();
                    $refno = get_post_meta( $pid, \'refno\', true );
                    $yr = get_post_meta( $pid, \'yr\', true );
                    $pieces = get_post_meta( $pid, \'pieces\', true );
                    $figures = get_post_meta( $pid, \'figures\', true );
                    ?>
                    <div class="my_box3">
                        <h3><?php the_title(); ?></h3>
                        <div class="padd10"><?php the_post_thumbnail(); ?></div>
                        <div class="padd10">
                            <ul>
                                <li> <?php _e(\'Referance Number\', \'AuctionTheme\');?>: <?php  echo $refno; ?></li>
                                <li> <?php _e(\'Year\', \'AuctionTheme\'); ?>: <?php echo $yr; ?></li>
                                <li> <?php _e(\'Pieces\', \'AuctionTheme\'); ?>: <?php echo $pieces; ?></li>
                                <li> <?php _e(\'Figures\', \'AuctionTheme\'); ?>: <?php echo $figures; ?></li>
                            </ul>
                    </div>

                              <?php}
            }
            wp_reset_query();
        die();
}

add_action(\'wp_ajax_show_all_products\', \'display_all_products\');
add_action(\'wp_ajax_nopriv_show_all_products\', \'display_all_products\');

function product_posts_where( $where ) {

        global $wpdb, $term;
        $searchTerms = explode(\' \', $term);

        $i = 1;
        $where .=" AND (";

        foreach ($searchTerms as $word) {
            if($i === 1) {
                $where .= " ({$wpdb->posts}.post_title LIKE \'%$word%\' OR {$wpdb->posts}.post_content LIKE \'%$word%\')";
            } else {
                $where .= " OR ({$wpdb->posts}.post_title LIKE \'%$word%\' OR {$wpdb->posts}.post_content LIKE \'%$word%\')";
            }
            $i++;
        }

        $where .=")";

    return $where;
}
将此添加到product\\u posts\\u,其中函数会导致整个操作失败,并从自定义字段中删除数据!

$where .= " OR ($wpdb->postmeta.meta_key = \'refno\' AND $wpdb->postmeta.meta_value = \'%$word%\')";

1 个回复
SO网友:Ralphonz

我设法解决了这种。。。

答案是对要搜索的每个自定义字段使用单独的搜索输入框。

如果有人偶然发现这一点,下面是我的工作解决方案。

function display_all_products() {

    global $term;
    $term = trim(strip_tags($_POST[\'s\']));
    $refnoterm = trim(strip_tags($_POST[\'ref\']));
    $yrterm = trim(strip_tags($_POST[\'yr\']));

    if(!empty($_POST[\'s\']) || !empty($_POST[\'ref\']) || !empty($_POST[\'yr\'])) {

            if(!empty($_POST[\'s\']))
            {
                add_filter( \'posts_where\' , \'product_posts_where\' );
            }

            if(!empty($_POST[\'ref\']))
            {
                $refno_meta = array(
                    \'key\' => \'refno\',
                    \'value\' => $refnoterm,
                    //\'type\' => \'numeric\',
                    \'compare\' => \'LIKE\'
                );
            }

            if(!empty($_POST[\'yr\']))
            {
                $yr_meta = array(
                    \'key\' => \'yr\',
                    \'value\' => $yrterm,
                    //\'type\' => \'numeric\',
                    \'compare\' => \'LIKE\'
                );
            }

        $meta_query = array(\'relation\' => \'OR\',);

        array_push($meta_query, $refno_meta);
        array_push($meta_query, $yr_meta);

            $args = array( 
                \'posts_per_page\' => 50,
                \'paged\' => 1, 
                \'post_type\' => \'product\', 
                \'order\' => \'DESC\',
                \'meta_query\' => $meta_query,
            );

            $the_query = new WP_Query( $args );

            remove_filter(\'posts_where\', \'product_posts_where\');


            if($the_query->have_posts()) {
                while ( $the_query->have_posts() ) {
                    $the_query->the_post();

                    $pid = get_the_ID();
                    $refno = get_post_meta( $pid, \'refno\', true );
                    $yr = get_post_meta( $pid, \'yr\', true );
                    $pieces = get_post_meta( $pid, \'pieces\', true );
                    $figures = get_post_meta( $pid, \'figures\', true );

                    $categories = get_the_terms( $pid, \'product_cat\' );

                    ?>

                    <div class="my_box3">
                        <h3><?php the_title(); ?></h3>
                        <div class="padd10"><?php the_post_thumbnail(); ?></div>
                        <div class="padd10">
                            <ul>
                                <li> <?php _e(\'Referance Number\', \'AuctionTheme\');?>: <?php  echo $refno; ?></li>
                                <li> <?php _e(\'Year\', \'AuctionTheme\'); ?>: <?php echo $yr; ?></li>
                                <li> <?php _e(\'Pieces\', \'AuctionTheme\'); ?>: <?php echo $pieces; ?></li>
                                <li> <?php _e(\'Figures\', \'AuctionTheme\'); ?>: <?php echo $figures; ?></li>
                                <li> <?php _e(\'Category and Theme\'); ?>:
                                    <?php
                                    foreach( $categories as $category) {
                                        echo $category->name . \', \';
                                    }
                                    ?>
                                </li>
                            </ul>
                        <label for="product">Select Product: <input type="radio" name="product" value="<?php the_ID(); ?>" /></label>
                    </div>

                    <?php   
                }
            } else {
                _e(\'There are no products that match your search, please try again.\', \'AuctionTheme\');
            }

            wp_reset_query();
            //wp_reset_postdata();

    } else {
        _e(\'You must enter something in at least one search box or select a product using the dropdown boxes and try again\', \'AuctionTheme\');
    }

    die();
}

add_action(\'wp_ajax_show_all_products\', \'display_all_products\');
add_action(\'wp_ajax_nopriv_show_all_products\', \'display_all_products\');

function product_posts_where( $where ) {

        global $wpdb, $term;
        $searchTerms = explode(\' \', $term);

        $i = 1;
        $where .=" AND (";

        foreach ($searchTerms as $word) {
            if($i === 1) {
                $where .= " ({$wpdb->posts}.post_title LIKE \'%$word%\' OR {$wpdb->posts}.post_excerpt LIKE \'%$word%\')";
            } else {
                $where .= " OR ({$wpdb->posts}.post_title LIKE \'%$word%\' OR {$wpdb->posts}.post_excerpt LIKE \'%$word%\')";
            }
            $i++;
        }

        $where .=")";

    return $where;
}

结束

相关推荐

If search matches taxonomy

如果搜索输入与分类名称完全匹配,是否可以将用户重定向到分类页面?例如,我有一个称为“参与者”的分类法。如果有人在搜索字段中输入“Tom Hanks”,而不是进入常规搜索页面,则会将他们重定向到Tom Hanks的分类页面。