0

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?

3
  • possible duplicate of How to create a custom search for custom post type? Commented Apr 7, 2014 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
    Commented Apr 7, 2014 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 Commented Apr 8, 2014 at 7:06

1 Answer 1

0

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');
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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