2

Here's my issue:

I have assigned a custom post type to certain categories. When I search for these categories, either through search or entering the url directly. However, if I assign a normal post type to a category, it becomes searchable and can be entered by the url directly. I thought I got everything in my custom post type code, but I must have missed something. Any help would be appreciated:

add_action('init', 'create_trainingplan_post_type');

function create_trainingplan_post_type() {
register_post_type('trainingplan', array(
    'labels' => array(
        'name' => 'Training Plans',
        'singular_name' => 'Training Plan',
        'add_new' => 'Add New Training Plan',
    'add_new_item' => 'Add New Training Plan',
    'edit' => 'Edit Training Plans',
        'edit_item' => 'Edit Training Plan',
        'new_item' => 'New Training Plan',
        'view_item' => 'View Training Plans',
        'search_items' => 'Search Training Plans',
        'not_found' => 'No Training Plans found',
        'not_found_in_trash' => 'No Training Plans found in Trash'
    ),
    'public' => true,
'exclude_from_search'=> false,
'rewrite' => array('slug' => 'my-custom-training-plan', 'with_front' => true),
'supports' => array(
    'title',
    'excerpt',
    'editor',
    'custom-fields',
    'revisions',
    'thumbnail',
    'author',
    'page-attributes'

    ),
    'taxonomies' => array('category', 'post_tag') // this is IMPORTANT
));
flush_rewrite_rules();
}

Thank you for the help so far, I've added comments from the answer I received so far to make it more clear in my question:

@ בניית אתרים I can actually search for the custom post type and find it. It's that the categories that are assigned only to custom post types cannot be searched or found. Basically, I am trying to enable a user to search by category, but categories that are assigned to only a custom post type are not appearing. For that matter, even if I type in the direct url of the category, it takes me to an error page: "Sorry, we can't find the category you're looking for at this URL. Please try selecting a menu item from above or to the side of this message to get where you..."

@ בניית אתרים I assigned the categories to the custom post type like you would a normal post. While writing the post, I ticked the categories I wanted included. When in the edit posts dashboard, it does confirm that categories are assigned to the cpt. However, when I go to the category url or search for posts within that category, cpt do not show.

Thanks again

4
  • TIP: You should never call flush_rewrite_rules(); inside an action run on init, it will cause the rules to flush on every page.
    – t31os
    Commented Jan 27, 2011 at 15:44
  • @t31os: He now knows when he should not flush them, but ... when should he instead?
    – kaiser
    Commented Mar 21, 2011 at 17:22
  • After changes to post type or taxonomy code(run once), or just view the permalinks page(what i do) after making changes(viewing the page causes a regeneration of rewrite rules). The code could be left in place, but ideally commented out and uncommented when changes are made to the post type registration code, but only for one page load(then commented out again).
    – t31os
    Commented Mar 22, 2011 at 8:53
  • first things first is you have two duplicates in your code i have to wait for approval on edit but i fixed that in your code. in the supports array you repeat title and excerpt which can lead to an error down the road Commented May 20, 2011 at 17:17

1 Answer 1

1

the default search function will only look for posts and pages so you need to add your post_type to the search and can do this in two ways

using the 'pre_get_posts' filter to add your post type before the query like this:

function Search_CPT_Filter($query) {
    $post_type = $_GET['type'];
    if (!$post_type) {
        $post_type = array('post','post type name');
    }
    if ($query->is_search) {
        $query->set('post_type', $post_type);
    };
    return $query;
};

add_filter('pre_get_posts','Search_CPT_Filter');

or, adding the post type to the search form in an hidden filed like this:

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

and in both cases just change 'post type name' to the name of your post type.

9
  • Sorry, that didn't solve the issue. I can actually search for the custom post type and find it. It's that the categories that are assigned only to custom post types cannot be searched or found. Basically, I am trying to enable a user to search by category, but categories that are assigned to only a custom post type are not appearing. For that matter, even if I type in the direct url of the category, it takes me to an error page: "Sorry, we can't find the category you're looking for at this URL. Please try selecting a menu item from above or to the side of this message to get where you..."
    – Jeff
    Commented Jan 20, 2011 at 1:55
  • ok then thats something else, how did you assign the categories to the post CPT?
    – Bainternet
    Commented Jan 20, 2011 at 2:20
  • $post_type = $_GET['type']; - No sanitization or checking the var is set first then?
    – t31os
    Commented Jan 20, 2011 at 14:16
  • I assigned the categories to the custom post type like you would a normal post. While writing the post, I ticked the categories I wanted included. When in the edit posts dashboard, it does confirm that categories are assigned to the cpt. However, when I go to the category url or search for posts within that category, cpt do not show.
    – Jeff
    Commented Jan 20, 2011 at 15:28
  • @t31os - not sure if that response was to me. If so, I am little unclear, I am not a php expert, just hacking my around. I appreciate the help
    – Jeff
    Commented Jan 20, 2011 at 15:32

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.