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 (i.e. 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');
$post
is – FluffyKitten