搜索和显示ACF中继器子字段
我有一个自定义的帖子类型叫玩家,它产生8个不同的玩家,每个玩家每个玩家有3个视频。每个视频都有一个标题。让我们说“玩家A - 游泳”“玩家A - 游泳”搜索和显示ACF中继器子字段
我该如何通过关键字进行搜索,以便每当我键入玩家A时,它只会向我显示玩家A的视频。玩家A将显示我搜索的所有视频结果
<?php
$s=($_GET["s"]); // Insert the searched keyword into a variable
// check if the repeater field has rows of data
if(have_rows('videos')):
// loop through the rows of data
while (have_rows('videos')) : the_row();
$video_1_title= get_sub_field('video_1_title');
$video_2_title= get_sub_field('video_2_title');
$video_3_title= get_sub_field('video_3_title');
$video_1= get_sub_field('video_1');
$video_2= get_sub_field('video_2');
$video_3= get_sub_field('video_3');
?>
<?php
endwhile;
endif;
?>
<?php if (have_posts()) :
while (have_rows('videos')) : the_row(); ?>
<div id="post-<?php the_ID(); ?>" class="videoPosts">
<?php
if(stripos($video_1_title, $_GET["s"])!== false) ||
(stripos($video_2_title, $_GET["s"])!== false) ||
(stripos($video_3_title, $_GET["s"])!== false)) :
?>
<div id="one"><?php echo $video_1?></div>
<div id="two"><?php echo $video_2?></div>
<div id="three"><?php echo $video_3?></div>
<?php endif; ?>
</div>
<?php
endwhile;
endif;
?>
的主要问题是,WordPress的默认情况下不搜索自定义字段,这就是为什么你必须编写自己的搜索代码的视频节目。
包括WP的搜索自定义字段实际上是复杂的,您通常会使用一个插件像Relevanssi,但你只想按标题搜索视频,那么我们能做到!
您的循环和逻辑有问题,但更大的问题是,Wordpress搜索仅返回关键字存在于标准WP字段(如帖子标题或内容)中而不是ACF字段的结果。
要继续你正在做的方式(即替换在您的search.php搜索):
1.从你的search.php用以下内容替换你上面的代码:
这使用WP_Query做一个全新的查询,只搜索视频标题的视频标题。
$s=($_GET["s"]);
// this will search all `player` posts for the search keyword in a custom field called video_%_title
// you don't need to specify video_1_title, video_2_title etc separately
$args = array(
'numberposts' => -1,
'post_type' => 'player',
'meta_query' => array(
array(
'key' => 'video_%_title',
'compare' => 'LIKE',
'value' => $s,
),
)
);
$the_query = new WP_Query($args);
// loop through the results
if($the_query->have_posts()):
while ($the_query->have_posts()) : $the_query->the_post();
// loop through all results to get the videos:
while (have_rows('videos')) : the_row(); ?>
<div id="post-<?php get_the_ID(); ?>" class="videoPosts">
<div id="one"><?php echo get_sub_field('video_1'); ?></div>
<div id="two"><?php echo get_sub_field('video_2'); ></div>
<div id="three"><?php echo get_sub_field('video_3'); ></div>
</div>
<?php
endwhile; // end video loop
endwhile; // end posts loop
endif;
wp_reset_query(); // Restore global post data stomped by the_post().
?>
注:
这显示发现的任何职位的所有视频,因为这是你自己的代码做什么。如果你想只显示与搜索关键字的,你可以这样做,而不是:
$video_1_title= get_sub_field('video_1_title');
$video_2_title= get_sub_field('video_2_title');
$video_3_title= get_sub_field('video_3_title');
<?php
if(stripos($video_1_title, $_GET["s"])!== false)) { ?>
<div id="one"><?php echo get_sub_field('video_1'); ?></div>
<?php
}
if(stripos($video_2_title, $_GET["s"])!== false)) { ?>
<div id="two"><?php echo get_sub_field('video_2'); ?></div>
<?php
}
if(stripos($video_3_title, $_GET["s"])!== false)) { ?>
<div id="three"><?php echo get_sub_field('video_3'); ?></div>
<?php
}
?>
2.添加到您的functions.php
(虽然它会工作,如果你有这在您的search.php页面)
这告诉WordPress的名为video_%_title
所有字段进行搜索,其中%为任意数量的
function player_video_posts_where($where) {
$where = str_replace("meta_key = 'video_%", "meta_key LIKE 'video_%", $where);
return $where;
}
add_filter('posts_where', 'player_video_posts_where');
wp_query选项不起作用,但第二个代码为我制作了视频部分。非常感谢。 –
欢迎堆栈溢出。你能确认这是什么网页吗?例如它是一个档案,一个单一的等?我试图找出你的'$ post'是 – FluffyKitten
它是一个search.php页面。我正在构建一个自定义的search.php页面,其中包含一个特定的设计,它将在单独的部分中显示所有视频,图像和说明。 我只是无法显示包含该特定值的单个帖子。例如,当您输入“John”时,应该在单独的部分中显示关于john的所有图像,视频和描述。现在它会显示所有视频,但会将“John's”视频放在搜索结果页面的顶部。 –
他们只是普通的职位,即你不使用自定义职位类型?我很确定我知道这个问题,但我只想在建议解决方案之前获得所有信息! – FluffyKitten