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 trying to put together some functions in wordpress which will allow me to add a custom post type and associate a default category and post on the posts page.

So far I have been able to create a custom post type, and as far as I can tell I am associating a default category. However, the posts do not show up in the post feed.

Here is the code I have pieced together:

add_action( 'init', 'create_prayer_request' );

function create_prayer_request() {
    register_post_type( 'prayer_request',
        array(
            'labels' => array(
                'name' => __( 'Prayer Requests' ),
                'singular_name' => __( 'Prayer Request' ),
                'add_new' => __( 'Add New Prayer Request' ),
                'add_new_item' => __( 'Add New Prayer Request' ),
                'edit' => __( 'Edit' ),
                'edit_item' => __( 'Edit Prayer Request' ),
                'new_item' => __( 'New Prayer Request' ),
                'view' => __( 'View Prayer Requests' ),
                'view_item' => __( 'View Prayer Request' ),
                'search_items' => __( 'Search Prayer Requests' ),
                'not_found' => __( 'No prayer requests found' ),
                'not_found_in_trash' => __( 'No prayer requests found in Trash' ),
            ),
            'public' => true,
        )
    );
}

function save_custom_post ( $post_id, $post = null ) {
    if ( !$post ) $post = get_post($post_id);
    if ( !current_user_can( 'edit_posts', $post->ID ) ) return;
    if ( 'prayer_request' != $post->post_type ) return;
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;

    wp_set_object_terms( $post->ID, 'prayer-request', 'prayer_request' );
}
add_action( "save_post", "save_custom_post", 10, 2 );
share|improve this question

migrated from stackoverflow.com Feb 3 '12 at 20:40

This question came from our site for professional and enthusiast programmers.

2 Answers 2

up vote 1 down vote accepted

The problem with 'save_post' is that it gets called when you actually change something on the post edit page, if you just click the update button without changing anything, it doesn't get called. So, i would say use 'pre_post_update' action hook instead of 'save_post' or use both. Also for debugging try put some test code in your custom function and test it to make sure it is being called.

share|improve this answer

After 'public' => true, add 'taxonomies' => array("category") and get this:

add_action( 'init', 'create_prayer_request' );

function create_prayer_request() {
    register_post_type( 'prayer_request',
        array(
            'labels' => array(
                'name' => __( 'Prayer Requests' ),
                'singular_name' => __( 'Prayer Request' ),
                'add_new' => __( 'Add New Prayer Request' ),
                'add_new_item' => __( 'Add New Prayer Request' ),
                'edit' => __( 'Edit' ),
                'edit_item' => __( 'Edit Prayer Request' ),
                'new_item' => __( 'New Prayer Request' ),
                'view' => __( 'View Prayer Requests' ),
                'view_item' => __( 'View Prayer Request' ),
                'search_items' => __( 'Search Prayer Requests' ),
                'not_found' => __( 'No prayer requests found' ),
                'not_found_in_trash' => __( 'No prayer requests found in Trash' ),
            ),
            'public' => true,
            'taxonomies' => array("category")
        )
    );
}

function save_custom_post ( $post_id, $post = null ) {
    if ( !$post ) $post = get_post($post_id);
    if ( !current_user_can( 'edit_posts', $post->ID ) ) return;
    if ( 'prayer_request' != $post->post_type ) return;
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;

    wp_set_object_terms( $post->ID, 'prayer-request', 'prayer_request' );
}
add_action( "save_post", "save_custom_post", 10, 2 );
share|improve this answer

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.