我通过博客的指定类别创建/合并了一个很酷的搜索。使用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)中的“键”与“值”进行比较吗?
谢谢大家,杰森。
答案 0 :(得分: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'].'%',
)
)
);