I want to query with keywords and display some related posts on custom taxonomy page, the related posts comes from a custom post type "gift". I have tried the code below, but it only worked for default post_type, how to make it to work for custom post_type? Many thanks!

    $querytitle = $reltopic; //$reltopic is query keywords
    $ids = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE UCASE(post_title) LIKE '%$querytitle%' AND post_type='gift' AND post_status='publish'");
    if ($ids) {
        'post__in' => $ids,
        'posts_per_page' => -1, 
        'caller_get_posts'=> 1
      $my_query = null;
      $my_query = new WP_Query($args);
      if( $my_query->have_posts() ) {
        //echo 'List of Posts';
        while ($my_query->have_posts()) : $my_query->the_post(); ?>
          <a href="<?php the_permalink() ?>" target="_blank"><?php the_title(); ?></a>
    wp_reset_query();  // Restore global post data stomped by the_post().

You actually need to pass the post type to your WP_Query arguments as well.

By default, post_type is set to post

A few notes:

  • caller_get_posts is long time depreciated, you should use ignore_sticky_posts

  • You should make sure that you have post ID's before passing it to post__in. Passing an empty array to post__in will return all posts regardless

  • Hi Pieter, i forgot to pass the post_type to WP_Query arguments. After added it, the code is working. Thank you very much! – Robin Feb 27 '16 at 4:11

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.