skip to Main Content

I’m working in Tv shows and movies theme. I have created new page template to display all movies and anther page template to show the tv shows (both custom post type).
My search form if any one use it from the index page working fine and shows the result in search.php
but if you are in movies page or anther pages the search form takes you to the index page (404 page not created yet)

I’m using this code in main header

<?php get_search_form();?>

this is my searchform.php

<form class="d-flex" role="search"  methode="get" action="<?php echo esc_url( home_url( '/' ) ) ?>">
    <input class="form-control"  type="text" name="s" id="search" placeholder="<?php echo ('search in site'); ?>" 
    aria-label="<?php echo ('search'); ?>" value="<?php echo get_search_query() ?>">
    <button class="btn btn-outline-success search-btn" type="submit"><i class="fa-solid fa-magnifying-glass"></i></button>
</form>

this is my search.php

<?php get_header(); 
$search = strtolower(get_search_query());
?> 


<?php 
$args = array(
  'taxonomy'      => array( 'series' ), // taxonomy name
  'hide_empty'    => true,
  'fields'        => 'all',
); 

$terms = get_terms( $args );

$count = count($terms);
if($count > 0){
   foreach ($terms as $term) { ?>
     get_template_part('/assests/series-temp');

   <?php } }
//code above for returning the custom term of each series post with different loop
 
$args= array(
  'post_type'=> array('movies'),
  'post_status' => 'publish',
  'posts_per_page' => -1,
  's' => $search
);
$titleQuery = new WP_Query( $args );
if ( $titleQuery->have_posts() ) {
  while ( $titleQuery->have_posts() ){ $titleQuery->the_post(); ?>
<?php

if ( get_post_type( get_the_ID() ) == 'movies' ) {
  get_template_part('/assests/movie-temp');
} ?>
              
  <?php }  wp_reset_postdata(); } ?>



<?php get_footer(); ?>

where is my mistakes?? please help

2

Answers


  1. Issue 1:
    in this their is a typo in this in place of "methode" write a "method"
    Issue 2:
    Adlso take care about the correct URL in the form action. If you are on the movies or TV shows page, you want the search to stay within that section of the site.

    Login or Signup to reply.
  2. Based on the provided code,Currently you are setting the action attribute to home_url(‘/’), which will always redirect the form submission to the homepage (/).

    for that the search form works correctly regardless of the page you are on, you should set the action attribute to the URL of your search results page (search.php).

    <form class="d-flex" role="search" method="get" action="<?php echo esc_url( home_url( '/search.php' ) ) ?>">
        <input class="form-control" type="text" name="s" id="search" placeholder="<?php echo __('Search in site'); ?>" aria-label="<?php echo __('Search'); ?>" value="<?php echo get_search_query() ?>">
        <button class="btn btn-outline-success search-btn" type="submit"><i class="fa-solid fa-magnifying-glass"></i></button>
    </form>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search