搜索和显示 ACF 转发器子字段
Search and Display ACF Repeater Sub Fields
我有一个名为 Players 的自定义 post 类型,它生成 8 个不同的播放器,每个播放器有 3 个视频。这些视频中的每一个都有一个标题。假设 "Player A - Running " "Player A - Swimming"
我如何通过关键字进行搜索,以便每当我输入播放器 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 仅搜索 returns 标准 WP 字段中存在关键字的结果(例如 post 标题或内容)而不是 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().
?>
注:
这会显示找到的任何 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');
我有一个名为 Players 的自定义 post 类型,它生成 8 个不同的播放器,每个播放器有 3 个视频。这些视频中的每一个都有一个标题。假设 "Player A - Running " "Player A - Swimming"
我如何通过关键字进行搜索,以便每当我输入播放器 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 仅搜索 returns 标准 WP 字段中存在关键字的结果(例如 post 标题或内容)而不是 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().
?>
注:
这会显示找到的任何 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');