0

I have added a new custom post type named: website_bookmarks

I plan on using it as my bookmarks manager (going to export all my bookmarks from Google Chrome into my custom post type) for the following reasons

  • Can search bookmarks easily
  • Can add tags (php, web design, etc...)
  • Can add descriptions to explain the bookmark better
  • Can view and sort however I choose
  • Can access from anywhere online
  • Can share my bookmarks with my readers ( will have option to mark a bookmark as Private or Public so I can be the only one to view the Private ones)
  • Sure there are other reasons to make this a good idea

Now my only problem so far is the search part. I cannot get my wordpress (latest release) to search the website_bookmarks post type for anything.

I have modified my search form with a hidden field

<input type="hidden" name="post_type" value="website_bookmarks" /> 

So on my search page in the URI I will see...

www.mydomain.com/?s=mysearchterm&post_type=website_bookmarks

The problem is it ONLY shows search results for my regular blog post and no results from my custom post type.

If I remove the s=mysearchterm from the URI so it is now

www.mydomain.com/?post_type=website_bookmarks

then it will show ALL the post from my custom post type. This shows that the website_bookmarks is the proper name for my post type, just the search part is not working correctly. It will retunr search results from regular post with no problem, just none from my website_bookmarks post type

It should not be showing any results for my blog post and ONLY results for my custom post type website_bookmarks

Can someone help me? I can not figure out why it will not work, the code I show above for modifying the search form, I found online from others saying that it worked for there blogs simply by changing the search form to what I did above.

Below is my whole search.php file as well

<?php get_header(); ?>
<?php if (have_posts()) : ?>
    <h1 class="pagetitle"><?php printf( __("Search results for <span>%s</span>", "vigilance"), get_search_query()); ?></h1>
    <img class="archive-comment"src="<?php echo get_template_directory_uri(); ?>/images/comments-bubble-archive.gif" width="17" height="14" alt="Comments"/>
    <div class="entries">
        <ul>
        <?php while (have_posts()) : the_post(); ?>
            <li><a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php esc_attr( sprintf( __( 'Permanent Link to %s', 'vigilance' ), the_title_attribute( 'echo=false' ) ) ); ?>"><span class="comments_number"><?php comments_number( '0', '1', '%', '' ); ?></span><span class="archdate"><?php the_time( __( 'M.j.y', 'vigilance' )); ?></span><?php the_title(); ?></a></li>
        <?php endwhile; /* rewind or continue if all posts have been fetched */ ?>
        </ul>
    </div><!--end entries-->
    <div class="navigation">
        <div class="alignleft"><?php next_posts_link( __( '&laquo; Older Entries', 'vigilance' )); ?></div>
        <div class="alignright"><?php previous_posts_link( __( 'Newer Entries &raquo;', 'vigilance' )); ?></div>
    </div><!--end navigation-->
<?php else : ?>
    <h1 class="pagetitle"><?php printf( __("Search results for '%s'", "vigilance"), get_search_query()); ?></h1>
    <div class="entry">
        <p><?php printf( __( 'Sorry your search for "%s" did not turn up any results. Please try again.', 'vigilance' ), get_search_query());?></p>
        <?php get_search_form(); ?>
    </div><!--end entry-->
<?php endif; ?>
</div><!--end content-->
<?php get_sidebar(); ?>
<?php get_footer(); ?>

I have even tried using this code below in my functions.php file with no luck...

function mySearchFilter($query) {
    $post_type = $_GET['post_type'];
    if (!$post_type) {
        $post_type = 'any';
    }
    if ($query->is_search) {
        $query->set('post_type', $post_type);
    };
    return $query;
};
add_filter('pre_get_posts','mySearchFilter');

Below is my code used to create the website_bookmarks post type, you can see that I have enabled query_var and publicly_queryable and have set exclude_from_search to true and false to test it both ways, with no luck

$bookmark_args = array(
        'labels' => $bookmark_labels,
        'public' => true,
        'publicly_queryable' => true,
        'exclude_from_search' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'query_var' => true,
        'rewrite' => array(
            'slug' => 'bookmark',
            'with_front' => false),
        'taxonomies' => array('bookmark_tags'),
        'capability_type' => 'post',
        'has_archive' => true,
        'hierarchical' => false,
        'menu_position' => 4,
        'can_export' => true,
        'supports' => array(
            'thumbnail', //website thumbnail
            'excerpt', //website description
            'title', //website title
            'custom-fields',
            'comments'
        )
    );   
    register_post_type('website_bookmarks', $bookmark_args);

3 Answers 3

2

did you try if (!isset($post_type)) or if(empty($post_type))?? For example I would try this:

    function mySearchFilter($query) {
    $post_type = $_GET['post_type'];

    if ($query->is_search) {
        if (!empty($post_type)) {
           $query->set('post_type', $post_type);
    }
  }
    return $query;
}

add_filter('pre_get_posts','mySearchFilter');

EDIT: Here is my working example code:

Here is my CPT used for testing:

add_action( 'init', 'register_cpt_website_bookmarks' );
function register_cpt_website_bookmarks() {
$labels = array(
'name' => _x( 'Bookmarks', 'website_bookmarks' ),
'singular_name' => _x( 'Bookmark', 'website_bookmarks' ),
'add_new' => _x( 'Add New', 'website_bookmarks' ),
'add_new_item' => _x( 'Add New Bookmark', 'website_bookmarks' ),
'edit_item' => _x( 'Edit Bookmark', 'website_bookmarks' ),
'new_item' => _x( 'New Bookmark', 'website_bookmarks' ),
'view_item' => _x( 'View Bookmark', 'website_bookmarks' ),
'search_items' => _x( 'Search Bookmarks', 'website_bookmarks' ),
'not_found' => _x( 'No bookmarks found', 'website_bookmarks' ),
'not_found_in_trash' => _x( 'No bookmarks found in Trash', 'website_bookmarks' ),
'parent_item_colon' => _x( 'Parent Bookmark:', 'website_bookmarks' ),
'menu_name' => _x( 'Bookmarks', 'website_bookmarks' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => false,
'supports' => array( 'title', 'editor' ),
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => false,
'publicly_queryable' => true,
'exclude_from_search' => false,
'has_archive' => true,
'query_var' => true,
'can_export' => true,
'rewrite' => true,
'capability_type' => 'post'
);
register_post_type( 'website_bookmarks', $args );
} 

Link: [link removed] (search for "Bookmark" or "Test" and if you remove the "post_type" and search for test you'll get more results)

4
  • Hello, yes I have tried similar and just tried this, still no luck though
    – JasonDavis
    Sep 30, 2011 at 0:12
  • Must be something wrong with your post type. I used the above code and modified my search bar and it works great. I did my test on wp 3.3 not sure what version you are using.
    – Brooke.
    Sep 30, 2011 at 19:16
  • I just tried it on a DIfferent theme, it works, however does not on my theme, so SOMETHING must be interfering on my theme to makew it not work, thanks for the test, I would of never thought to check if it were just 1 theme or another not working
    – JasonDavis
    Sep 30, 2011 at 22:27
  • I would look for other pre_get_posts in your theme or other things that might break it
    – Brooke.
    Oct 1, 2011 at 6:25
2

first of all, setting

'exclude_from_search' => true,

will exclude the CPT from showing in search results. It must be set to false. The CPT Generator by Themergency will be useful for you to quickly write your CPTs.

To include CPTs in archives or search I usually use this, which is slightly different to yours:

function include_custom_post_types( $query ) {
    $custom_post_type = get_query_var( 'post_type' );

    if ( is_archive() ) {
        if ( empty( $custom_post_type ) ) $query->set( 'post_type' , get_post_types() );
    }

    if ( is_search() ) {
        if ( empty( $custom_post_type ) ) {
            $query->set( 'post_type' , array(
                'post', 'page', 'yourcpt'
                )
            );
        }
    }

    return $query;
}
add_filter( 'pre_get_posts' , 'include_custom_post_types' );

Hope this works for you.

1
  • Hello, I had changed to this 'exclude_from_search' => true, just to test, it is now set to false again but that makes no difference in my problem. Thank you for the link to CPT Generator, that is nice! I tried your code above, changed 'post', 'page', 'yourcpt' to 'post', 'page', 'website_bookmarks' however, my search results still do not return any results from my website_bookmarks CPT, it is very strange to me that nothing is working
    – JasonDavis
    Sep 30, 2011 at 0:46
0

Maybe

'exclude_from_search' => false

and

and re-save permalinks to flush cache

Your Answer

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

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