is there any way to include custom post title as search term. I create custom post types - VHS, DVD, Blu Ray. Then in search box when I write DVD I want to list all posts from custom post type DVD.

Thank you all in advance :)


If you register the post type with public set to true, the titles will be included automatically. You shouldn't have to do anything to make this happen. Something as simple as this from the Codex:

function codex_custom_init() {
    $args = array( 'public' => true, 'label' => 'Books' );
    register_post_type( 'book', $args );
add_action( 'init', 'codex_custom_init' );

If you aren't sure you can test it with this:

function test_custom_types_inclusion($where) {
  if (is_search()) {

That will completely break you search but you can see the raw WHERE clause to confirm that your CPT is included. Look for the post_type IN ( part.

That covers this question:

is there any way to include custom post title as search term[?]

But I think you meant that you want to search the names you use to register the types, not the titles. To include all posts listed in a custom post type-- and I don't see the logic of doing that, as you are going to get a potentially huge number of results, making the search more or less useless-- something like this...

function custom_types_inclusion_wpse_81742($where) {
  if (is_search()) {
    global $wpdb;
    $where .= " OR {$wpdb->posts}.post_type = 'your-type-name'";
  return $where;

But to have your search terms respected you will have to get more complicated.

function custom_types_inclusion_wpse_81742_v2($search) {
  global $wpdb;
  $terms = preg_match_all('/%([^%]+)%/',$search,$matches);
  $ts = array();
  if (!empty($matches[1])) {
    $matches = array_unique($matches[1]);
    if (!empty($matches)) {
      foreach ($matches as $m) {
        $ts[] = "{$wpdb->posts}.post_type LIKE '%{$m}%'";
    if (!empty($ts)) {
      $search .= "OR ((".implode(') OR (',$ts).'))';
  return $search;

Untested but I think that should do it.

Again, I don't see the logic. Results aren't going to mean much.

  • Thank you for the fast answer. It works, but I have one question. Your example work only with one post type - OR {$wpdb->posts}.post_type = 'your-type-name' How to make it check more than one custom post type? – Dido Kotsev Jan 16 '13 at 3:22
  • Add more OR conditions or use IN the way WordPress does for post_type – s_ha_dum Jan 16 '13 at 4:49
  • I try with this code: {$wpdb->posts}.post_type IN ( 'vhs', 'dvd' ) But now when I write in search box - DVD it shows all post from both post types VHS and DVD. – Dido Kotsev Jan 16 '13 at 14:08
  • Added a new version. – s_ha_dum Jan 16 '13 at 15:02
  • Many thanks for the new code, but I don't understand, where can I define which custom post titles can be used as search terms? One friend has site, with many custom post types. Many of the new users don't use navigation (mainly because it's huge), but use search instead and they often use custom post titles as search terms. That's why we want to add this functionality. – Dido Kotsev Jan 16 '13 at 19:10

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.