0

I'm trying to create a search box on an archive page which shows on Custom post type.

I've followed this guide to implement the search box on the archive page:

http://wpsnipp.com/index.php/template/create-multiple-search-templates-for-custom-post-types/

So I've created a search.php file and I put this code in it:

<?
/* Template Name: Search Results */
$search_refer = $_GET["post_type"];
if ($search_refer == 'spec-needs-res') { load_template(TEMPLATEPATH . '/template_search_spec_needs.php'); }
else { load_template(TEMPLATEPATH . '/template_search_default.php'); };
?>

I've created a template_search_spec_needs.php file and I put this code (loop) in it:

  <?php
    $args = array( 'post_type' => 'spec-needs-res', 'posts_per_page' => -1, 's' => $s, 'paged' => $paged );
    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post();
  ?>

        <div class="span4 spec-needs-list-single">
            <div class="spec-needs-title"><?php the_title(); ?></div>
            <div><?php the_excerpt(); ?></div>
            <div class="plus-btn-spec-needs-cont"><a href="<?php the_permalink(); ?>"><button class="plus-btn-spec-needs">+</button></a></div>
        </div>

  <?php
    endwhile;
    wp_reset_query();
  ?>

I've added this code on the archive-spec-needs-res.php

<form id="searchform" action="<?php bloginfo('home'); ?>/" method="get">
        <input id="s" maxlength="150" name="s" size="20" type="text" value="" class="txt" />
        <input name="post_type" type="hidden" value="spec-needs-res" />
        <input id="searchsubmit" class="btn" type="submit" value="Search" />
</form>

The process seems to be fine but every time I try searching something I get an empty page, the template is picked up but I have no posts so I never get results.

I've followed the guide step by step, any advice?

UPDATE

I noticed that the search box gives me results just when I do the search but without parameters and obviously, I get all the posts, but once I put a parameter I don't get results anymore so the search box seems to work but I doesn't when I use a keyword to search a custom post.

This is the URL that I get when I do a search with the word "webinar":

http://www.matrix-test.com/edtech3/?s=webinar&post_type=spec-needs-res

Is the URL correct?

I have a custom post type with "Webinar" as a title and another one with "Webinar" as tag and I put webinar somewhere in text as well.

Here the code I used to create the custom post type:

/****************************************
 * Add custom taxonomy for Specific Needs *
 ****************************************/

add_action('init', 'spec_needs_cat_register');

function spec_needs_cat_register() {
$labels = array(
    'name'                          => 'Specific Needs Categories',
    'singular_name'                 => 'Specific Needs Category',
    'search_items'                  => 'Search Specific Needs Categories',
    'popular_items'                 => 'Popular Specific Needs Categories',
    'all_items'                     => 'All Specific Needs Categories',
    'parent_item'                   => 'Parent Specific Needs Category',
    'edit_item'                     => 'Edit Specific Needs Category',
    'update_item'                   => 'Update Specific Needs Category',
    'add_new_item'                  => 'Add New Specific Needs Category',
    'new_item_name'                 => 'New Specific Needs Category',
    'separate_items_with_commas'    => 'Separate Specific Needs categories with commas',
    'add_or_remove_items'           => 'Add or remove Specific Needs categories',
    'choose_from_most_used'         => 'Choose from most used Specific Needs categories'
    );

$args = array(
    'label'                         => 'Specific Needs Categories',
    'labels'                        => $labels,
    'public'                        => true,
    'hierarchical'                  => true,
    'show_ui'                       => true,
    'show_in_nav_menus'             => true,
    'args'                          => array( 'orderby' => 'term_order' ),
    'rewrite'                       => array( 'slug' => 'spec-needs-tax', 'with_front' => true, 'hierarchical' => true ),
    'query_var'                     => true
);

register_taxonomy( 'spec_needs_cat_register', 'spec-needs-res', $args );
}

/*****************************************
 * Add custom post type for Specific Needs *
 *****************************************/

add_action('init', 'spec_needs_res_register');

function spec_needs_res_register() {

    $labels = array(
        'name' => 'Specific Needs',
        'singular_name' => 'Specific Needs',
        'add_new' => 'Add New',
        'add_new_item' => 'Add New Specific Needs',
        'edit_item' => 'Edit Specific Needs',
        'new_item' => 'New Specific Needs',
        'view_item' => 'View Specific Needs',
        'search_items' => 'Search Specific Needs',
        'not_found' =>  'Nothing found',
        'not_found_in_trash' => 'Nothing found in Trash',
        'parent_item_colon' => ''
    );

    $args = array(
        'labels' => $labels,
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'query_var' => true,
        'has_archive' => true,
        'rewrite' => array( 'slug' => 'spec-needs-res', 'with_front' => true ),
        'capability_type' => 'post',
        'menu_icon' => 'dashicons-images-alt2',
        'menu_position' => 14,
        'supports' => array('title', 'excerpt', 'editor','thumbnail') //here you can specify what type of inputs will be accessible in the admin area
      );

    register_post_type( 'spec-needs-res' , $args );
}
  • Hmm... How does your register_post_type Look? – kuchenundkakao Mar 28 '18 at 15:54
  • I just added my custom post type's code to the answer, thanks a lot – Crashy Mar 28 '18 at 15:59
0

In your template_search_spec_needs.php, remove the custom query. The parameters in your url (s=&post_type=spec-needs-res) should work with the "normal" loop just fine.

So try changing your template_search_spec_needs.php to something like this:

<?php

    while ( have_posts() ) : the_post();
  ?>

        <div class="span4 spec-needs-list-single">
            <div class="spec-needs-title"><?php the_title(); ?></div>
            <div><?php the_excerpt(); ?></div>
            <div class="plus-btn-spec-needs-cont"><a href="<?php the_permalink(); ?>"><button class="plus-btn-spec-needs">+</button></a></div>
        </div>

  <?php
    endwhile;

And you should be just fine ;)

  • Hi, thanks for helping me, I've tried using the simple loop but now it doesn't even seem to give me results when I do a research with an empty query. I've used the same template for the default search for the default posts (template_search_default.php) and it seems to work properly. But for the for the custom template, it doesn't seem to pick up the custom post type because I always get 0 results. Any Idea? – Crashy Mar 28 '18 at 15:53
  • Not quite Sure, as i am in mobile right now. You could try and change the Post Type identificator to use underscores instead of minus. Else i have to try to Help tomorrow when i'm Back at Work... – kuchenundkakao Mar 28 '18 at 16:02
  • Ok, I'll try with that one in that in the meantime, thanks again! – Crashy Mar 28 '18 at 16:35
  • Just noticed that Relevanssi plugin was used and obviously created a conflict with WP search. Just deactivated it and seems to work properly. Thanks for your help! – Crashy Mar 29 '18 at 10:01
  • Hi, is there a way to print more than one custom post type? I want to print these custom post type: catalogues-res, forms-res and spec_needs_res. With the code above I'm able to print just spec_needs_res. Any Idea? – Crashy Mar 30 '18 at 12:50

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.