Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am new to wordpress and a beginner coder, and i was searching around the net, found solutions but still does not work.

I have custom post type named dlm_download, and my custom search template not working

I added the following to my functions.php file - the code is from this answer by @Ronald.

function template_chooser($template)
{
  global $wp_query;
  $post_type = get_query_var('post_type');
  if( $wp_query->is_search && $post_type == 'dlm_download' )
  {
    return locate_template('archive-download.php');
  }
  return $template;
}
add_filter('template_include', 'template_chooser');

Created the archive-download.php

and when i preform my custom search, i get the url like this mysite.com/?dlm_download=699 nothing happens i just get redirected to my home page.

Could please someone tell what i am doing wrong?

share|improve this question
    
possible duplicate of How to create a custom search for custom post type? –  Pieter Goosen Apr 7 at 17:39
    
First, keep in mind that any content on this site is CC-BY-SA 3.0 licensed. This means that you can't randomly copy/paste code around without stating the source and the copyright owner/creator of that code. I edited your question to clearly state that (thanks @PieterGoosen for the link). Second, please clarify what you expected, where the archive-download.php file is located, etc. All in an edit. –  kaiser Apr 7 at 18:13
    
@kaiser sorry and thank you. ( Pieter Goosen) Not a possible duplicate, i already told i was searching a lot did not work including that –  user1512390 Apr 8 at 7:06

1 Answer 1

up vote 0 down vote accepted

You can simply include a custom search function into your search by using pre_get_posts

You can do something like this

function search_filter($query) {
  if ( !is_admin() && $query->is_main_query() ) {
    if ($query->is_search) {
      $query->set('post_type', 'your-custom-post-type' );
    }
  }
}

add_action('pre_get_posts','search_filter');
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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