简体   繁体   中英

Search and Display ACF Repeater Sub Fields

I have a custom post type called Players which is generating 8 different players with each of them having 3 videos per player. Each of these video has a title. Let's say "Player A - Running " "Player A - Swimming"

How can I search via keyword in such a way that whenever I type in Player A it only shows me the videos of the player A. Right now whenever type Player A it displays me all the videos in the search Result

<?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;
?>

The main problem is that Wordpress doesn't search custom fields by default, which is why you have had to write your own search code for the video titles.

Including custom fields in WP's search is actually complicated and you would normally use a plugin like Relevanssi, but as you only want to search for videos by title, then that we can do!

You have problems with your loops and logic, but the bigger problem is that Wordpress search only returns results where the keyword exists in standard WP fields (such as post title or the content) and not the ACF fields.

To continue the way you are doing it (ie replacing the search in your search.php):

1. Replace the code you have above from your search.php with the following:

This uses WP_Query to do a brand new query that searches only the video title for your just for the video titles.

$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(). 
?>

Note:

This displays all the videos in any posts that were found, because that's what your own code does. If you want to show just the ones with the search keyword, you can do this instead:

$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. Add this to your functions.php

(Although it will work if you include it in your search.php page)
This tells Wordpress to search in all fields called video_%_title where % is any number

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');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM