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 using these helper classes to create a custom post but wp_query does not return any instances. Checked the source and as far as I can see the public has been set to true.

The query is the following:

  $numFetch=$multiple?100:1;
  $args = array( 'posts_per_page' => $numFetch,
      'search_post_title' => $data_source_name,
      'post-type' => 'any',
      'orderby' => 'title', 'order' => 'ASC'
      );
  $args['search_post_title'] = $multiple?
    '.post_title LIKE \'' . esc_sql(like_escape($data_source_name)) . '%\'':
    '.post_title = \'' . esc_sql(like_escape($data_source_name)) . '\'';
  add_filter('posts_where', 'title_filter', 10, 2);
  $loop = new WP_Query($args);
  while ($loop->have_posts()) : $loop->the_post();
    var_dump($post);
  endwhile;

In funcitons.php I have

function title_filter($where, &$wp_query) {
  global $wpdb;
  if ($search_term = $wp_query->get('search_post_title')) {
    $where .= ' AND ' . $wpdb->posts . $search_term;
  }
  return $where;
}

When I add a post with the same title it does show up. I will try to manually create a custom post and see if that shows up (leaving default public to true) but not sure if that is the problem. I'd like it to not show up in searches anyway unless I specify it but could not find a way to do this either (like 'exclude_from_search'=>true)

[UPDATE]

Added the following code in functions.php:
function codex_custom_init() {
    $args = array(
      'public' => true,
      'label'  => 'Books'
    );
    register_post_type( 'book', $args );
}
add_action( 'init', 'codex_custom_init' );

Then added a Book with the title: "a book", then changed the query code to:

$args['search_post_title'] = '.post_title LIKE \'%a book%\'';

But no items are returned.

If I add a post with the title "a book" then it does show up. Looks like 'post-type' => 'any' isn't doing what it's supposed to do.

share|improve this question

2 Answers 2

Just spent another hour trying to debug something because the shift key on my keyboard doesn't always work.

post-type != post_type

There should be an under score between post and type, not a hyphen.

share|improve this answer

Try adding this and see if it works:

function filter_search( $query ) {
    if ( $query->is_search ) {
        $query->set('post_type', array('book'));
    };
    return $query;
};
add_filter( 'pre_get_posts', 'filter_search' );
share|improve this answer
    
Thank you for your answer, I was trying to search for any post type including custom post(s). If I add book and later want to search for writer than that filter would prevent it from showing up. 'post_type' => 'any' should let me find the post I'm looking for and does but since I made a typo it didn't –  HMR Oct 19 '13 at 2:09

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.