在自定义搜索中包含高级自定义字段 (ACF) - WordPress

问题描述 投票:0回答:2

我已经通过我的博客的指定类别创建/混合了一个很酷的搜索。使用 Ajax 加载结果而无需重新加载。

当我搜索时 - 无论我搜索什么词。我收到所有帖子。

我使用 ACF 来表示内容和作者。我还使用字段featured_product_title 来引用产品。这些字段在我的页面中使用如下:

<?php if ( have_rows('case_study_page_content') ): ?>
    <?php
        while (have_rows('case_study_page_content')): the_row();
        $title = get_sub_field('title');
        $author = get_sub_field('author');
        $content = get_sub_field('content');
    ?>
        <div class="">
            <h1 class=""><?php echo $title; ?></h3>
            <h3 class=""><?php echo $author; ?></h4>
            <p><?php echo $content; ?></p>
        </div>
    <?php endwhile; ?>
<?php endif; ?>


<?php
    while (have_rows('featured_products')): the_row();
    $featured_product_title = get_sub_field('featured_product_title', 'featured_products');
?>

考虑到这些,我当前的搜索如下所示(functions.php):

// CASE STUDY SEARCH 
function my_search(){
    $args = array(
        'orderby' => 'date',
        'order' => $_POST['date']
    );
  
    if( isset( $_POST['s'] ) ):
/*
        $args = array(
            'post_type' => 'post',
            'posts_per_page' => -1,
            's' => $_POST['s']
        );
*/

        if( have_rows('case_study_page_content') ):

            while( have_rows('case_study_page_content') ) : the_row();

                $title = get_sub_field('title');
                $author = get_sub_field('author');
                $content = get_sub_field('content');

                    $args = array(
                        'post_type' => 'post',
                        'posts_per_page' => -1,
                        'meta_query'    => array(
                            'relation'      => 'OR',
                            array(
                                'key'       => $title,
                                'compare'   => 'like',
                                'value'     => '%'.$_POST['s'].'%',
                            ),
                            array(
                                'key'       => $author,
                                'compare'   => 'like',
                                'value'     => '%'.$_POST['s'].'%',
                            ),
                            array(
                                'key'       => $content,
                                'compare'   => 'like',
                                'value'     => '%'.$_POST['s'].'%',
                            )
                        )
                    );

            endwhile;

        endif;

        $query = new WP_Query($args);

        if( $query->have_posts() ):

            while( $query->have_posts() ):
                $query->the_post();

                echo "<article class=\"post-box " . get_post_class() . "\">";

                    echo "<a href=\"" . get_the_permalink() . "\" class=\"box-link\"></a>";

                    $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID), 'thumbnail' );
                    echo "<img src=\"" . $url . "\" />";

                    echo "<h2>" . get_the_title() . "</h2>";

                    $case_study = get_field('case_study_page_content');
                    
                    if( $case_study ):
                        
                        while( have_rows('case_study_page_content') ): the_row();
                            $case_study_author = get_sub_field('author');

                            echo "<p>" . $case_study_author . "</p>";
                        endwhile;
                    endif;

                echo "</article>";

            
            endwhile;
            wp_reset_postdata();

        else :

            echo 'No case studies found';

        endif;

    die();

    endif;
}

add_action('wp_ajax_customsearch', 'my_search');
add_action('wp_ajax_nopriv_customsearch', 'my_search');

我想我的问题是如何将 ACF 添加到 $args 数组中...?

如何将我的

WP_Query($args)
中的“键”与“值”进行比较?

php ajax wordpress search
2个回答
0
投票

测试一下,但没有说服力

// args
$args = array(
    'numberposts'   => -1,
    'post_type'     => 'post',
    'meta_query'    => array(
        'relation'      => 'OR',
        array(
            'key'       => 'case_study_page_content_title',
            'compare'   => 'like',
            'value'     => '%'.$_POST['s'].'',
        ),
        array(
            'key'       => 'case_study_page_content_author',
            'compare'   => 'like',
            'value'     => '%'.$_POST['s'].'%',
        ),
        array(
            'key'       => 'case_study_page_content_content',
            'compare'   => 'like',
            'value'     => '%'.$_POST['s'].'%',
        )
    )
);

0
投票
function custom_search_query( $query ) {
    if ( !is_admin() && $query->is_search ) {
        $result = $query->query_vars['s'];
        $query->query_vars['s'] = '';
        $query->set('meta_query', array('relation' => 'OR',
            array(
                'key'     => 'acf_name', // ACF FIELD NAME OR POST META
                'value'   => $result,
                'compare' => 'LIKE',
            )
        ));
        $query->set('post_type', 'post'); // optional POST TYPE
    }
}
add_filter( 'pre_get_posts', 'custom_search_query');
© www.soinside.com 2019 - 2024. All rights reserved.