[php] search wp

Viewer

copydownloadembedprintName: search wp
  1. (CPT - autor)
  2.  
  3. header.php
  4. ------------
  5.  
  6. <form role="search" method="get" id="searchform" class="searchform" action="<?php echo esc_url( home_url( '/' ) ); ?>">
  7.         <label for="s">Search for:</label>
  8.         <input type="text" value="" name="s" id="s" />
  9.         <input type="hidden" value="autor" name="post_type" id="post_type">
  10.         <input type="submit" id="searchsubmit" value="Search" />
  11. </form>
  12.  
  13.  
  14. search.php
  15. ------------
  16.  
  17. <?php
  18.                         /* Start the Loop */
  19.                         global $post;
  20.  
  21.                         $post_type = get_query_var('post_type');
  22.  
  23.                         $args = array(
  24.                                 's' => esc_html( get_search_query( false ) ),
  25.                                 'post_type' => $post_type,
  26.                                 'posts_per_page' => 2
  27.                                 );
  28.  
  29.                         $myposts = get_posts( $args );
  30.  
  31.                         foreach ( $myposts as $post ) : setup_postdata( $post );
  32.  
  33.                                 get_template_part( 'template-parts/content', 'search' );
  34.  
  35.                         endforeach;
  36.                        
  37.                         wp_reset_postdata();
  38.  
  39.  
  40.                 ?>
  41.                

Editor

You can edit this paste and save as new:


File Description
  • search wp
  • Paste Code
  • 10 Aug-2021
  • 882 Bytes
You can Share it: