WordPress.org

Ready to get started?Download WordPress

Forums

Custom Post Types adding info to Taxonomy (3 posts)

  1. MaxMcNally
    Member
    Posted 3 years ago #

    I'm trying to set up a site with a custom post type called 'Village Life', with a matching taxonomy called 'village'. Here's the code I used to register my post type -

    function create_post_type() {
    	register_post_type('Village',
    		array(
    			'labels' => array(
    					'name'=> __('Villages'),
    					'singular_name'=> __('Village')
    					),
    
    			'public' => true,
    			'has_archive' => true,
    			'rewrite' => array(
    				'slug' => 'village-life'
    
    			)
    
    		)
    	);
    
    }

    It works fine, and it shows up in the dashboard. When a user creates a 'Village Life' post, I'd like to add the title of the post (as a term) to the taxonomy 'village', so there will be a controlled list of 'villages' (There should be no other way for a user to add a village to the taxonomy, without creating a "Village Life' post. Here's the function for that -

    function create_post_type() {
    	register_post_type('Village',
    		array(
    			'labels' => array(
    					'name'=> __('Villages'),
    					'singular_name'=> __('Village')
    					),
    
    			'public' => true,
    			'has_archive' => true,
    			'rewrite' => array(
    				'slug' => 'village-life'
    
    			)
    
    		)
    	);
    
    }

    And here are the hooks I'm using -

    add_action('init','create_post_type');
    add_action('edit_post', 'create_village_callback');
    add_action('save_post ', 'create_village_callback');
    add_action('publish_post ', 'create_village_callback');
    add_action('edit_page_form ', 'create_village_callback');

    None of them seem to work. I've gotten the echo statement to show up on autodraft saving, but it doesn't run (or seem to run) when I click publish. Also, the titles are not showing up in my taxonomy. Anyone able to see what I'm doing wrong? I appreciate the help in advance!

  2. PatJ
    Member
    Posted 2 years ago #

    This is exactly what I'm trying to do and save_post is working fine when editing but isn't called on adding a new one of post-type. I've posted a cry for help on this forum. I'm usung version 3.1.3

    This is my code in save_post which works beautifully when editing a custom post (check at start of save_post function if this is right post-type and not a revision or autosave)

    $tslug = $post->post_name;
    	$tname = $post->post_title;
    
    	$tax = 'mytaxonomy'; // in your case village
    	$termno = term_exists( $tslug, $tax );
    // array is returned if taxonomy is already in
    // if term doesn't exist, add term as subsidiary of parent. Use 0 for parent if no parent.
    		if (!isset($termno)) {
    			wp_insert_term(
    			$tname, // the term
    			$tax, // the taxonomy
    			// description = text bit about term
    			array(
    				'description'=> $tname,
    				'slug' => $tslug,
    				'parent'=> 0
    				)
    			);
    	}
  3. MaxMcNally
    Member
    Posted 2 years ago #

    I managed to get it to work by adding a filter instead of an action -

    add_filter('wp_insert_post_data','create_taxonomy_callback');

    and here is my callback function -

    if($data['post_type'] == 'revision' || $data['post_status'] == 'auto-draft') {
    			//Ignore revisions
    			return $data;
    		}
    
    		if($data['post_type'] == 'villagelife' && !term_exists( $data['post_title'], 'villagelife' )){
    			wp_insert_term(
    			$data['post_title'], // the term
    				  'village' // the taxonomy
    			);
    		}

    Hope that helps!

Topic Closed

This topic has been closed to new replies.

About this Topic