I'm trying to create a custom search box that will show posts from specific category by my choise.

Every term that will be submitted in the search box will apply the search in one category data, which will display only posts that match the search terms inside the category.

For example, I have a music category in my WordPress site, and the regular search box show results from all categorys, then a custom category search box will show results from music category only.

If you can help me please reply Shay.


You can use WP_query() to narrow down the results of your search:

<?php $args = array(
    's' => $_GET['s'],
    'post_type' => array( 'post', 'page' ),
    'post_status' => 'publish',
    'category_name' => 'music',
    'posts_per_page' => -1
$custom_search = new WP_Query( $args );
if ( $custom_search->have_posts() ) {
    while ( $custom_search->have_posts() ) : $custom_search->the_post(); ?>
         <div class="entry-content">
            <h2 class="title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
            <?php the_excerpt(); ?>

    <?php endwhile;

} else { ?>
    <h2>Your search didn't return any results.</h2>
<?php } ?>
| improve this answer | |
  • Hello :) Thanks for responding! where do I add this code? can you please give me example? – Shay S Apr 5 '17 at 20:50
  • It depends on how you wanna go about it exactly, but you would place it in search.php – Pim Apr 6 '17 at 2:12
  • i'm trying to show this search in a certain page, so should I add it in search.php, and then what shortcode will display this? – Shay S Apr 9 '17 at 9:14
  • For the search form, you need to use get_search_form(). For the results, you need the code above. – Pim Apr 9 '17 at 15:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.