Support » Plugins » Hacks » Getting stuck on creating a default category for a custom post type

  • I am looking for a solution to a seemingly simple problem.

    I have created a custom post type like is:
    add_action( ‘init’, ‘create_praise_report’ );

    function create_state() {
    	register_post_type( 'state',
    			'labels' => array(
    				'name' => __( 'States' ),
    				'singular_name' => __( 'State' ),
    			'public' => true,

    I have additional arguments in the array, but am keeping my code simple for discussion.

    I want each post under the “State” custom post type to publish with a default category of “US States”. While novicely searching the codex and trying different things I found this snippet:
    register_taxonomy_for_object_type($taxonomy, $object_type);
    Of which I changed to this:
    register_taxonomy_for_object_type('us-states', 'state');
    This did not not produce what I wanted. In fact I received an error after saving functions.php.

    Thank you in advance for your help.

Viewing 7 replies - 1 through 7 (of 7 total)
  • Think wp_set_object_terms would do the trick when used inside the save_post action’s callback.

    function my_cpt_save_post ( $post_id, $post = null ) {
    	if ( !$post ) $post = get_post($post_id);
    	if ( !current_user_can( 'edit_posts', $post->ID ) ) return;
    	if ( 'state' != $post->post_type ) return;
    	if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
    	wp_set_object_terms( $post->ID, 'us-states', 'state' );
    add_action( "save_post", "my_cpt_save_post"), 10, 2 );


    just make sure the term actually exists first

    [edit] and you’ll probably want to check if the post has any terms already assigned before you give it the default.

    Thank you for your detailed reply. By assigned terms, what do you mean?

    Just whether or not you want the default term to be given to every post regardless. Like, if you gave it the term Ohio in the editor, would you still want it to be included in US-States?

    Bit too much detail here but it’s bad for SEO to have it under multiple hierarchical categories/terms as search engines see that as duplicate content.

    When I add the code I get an error:
    Parse error: syntax error, unexpected ',' in /home/prayerpi/public_html/wp-content/themes/twentyeleven/functions.php on line 687
    line 687 is:
    add_action( "save_post", "my_cpt_save_post"), 10, 2 );

    I’m sure what you gave me works perfectly because when I comment out “add_action” it is fine.

    Can you think of anything that may be wrong?

    eh eh… good stuff.

    add_action( "save_post", "my_cpt_save_post", 10, 2 );

    Had an extra parenthesis in my example that left the 10, 2); part dangling by its lonesome.

    –{ As I rush to check that I didn’t just put the same error in a client’s websites )– lol

    Thank you. The error is gone. You have been very helpful and I have one more question about this if you can manage an answer.
    All of this seems to work except that the custom posts don’t “publish” to the front of the site. Do you have any ideas? I cannot find anything in all of the codex about this. Any ideas?

    They did before right? I don’t think setting that save action would do it.

    [edit: do a search for one of the titles just to make sure]

    Method 1 — Add this to your register_post_type

    'has_archive' => 'states',
    'rewrite' => array('slug' => 'state'),

    and add a button to your menu for your.domain.tld/states/ — I believe that will automatically go through the “template hierarchy” and use archive.php, I haven’t gone that route in awhile.

    Method 2 — Bit more complex but lets you give the page an intro
    Still add those lines but set ‘has_archive’ => false. Create a new page and set it’s template to a custom page template. Then, inside that template, below the get_template_part do a WP_Query while setting it’s post_type to ‘states’. And again, add a menu button to that page.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Getting stuck on creating a default category for a custom post type’ is closed to new replies.