搜索和显示ACF中继器子字段

时间:2017-09-01 17:34:15

标签: php wordpress advanced-custom-fields

我有一个名为Players的自定义帖子类型,它会生成8个不同的玩家,每个玩家每个玩家有3个视频。这些视频中的每一个都有一个标题。让我们说"玩家A - 跑步" "玩家A - 游泳"

我如何通过关键字进行搜索,每当我输入玩家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;
?>

1 个答案:

答案 0 :(得分:0)

主要问题是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');
相关问题