Populate Taxonomy from Custom Posts

I have a custom post I am looking to categorize via a custom taxonomy. The taxonomy itself I want to be dependent on the

I have a custom post I am looking to categorize via a custom taxonomy. The taxonomy itself I want to be dependent on the items of another custom post type. Is it possible to populate a taxonomy via the items of a custom post type, or is it something I have to do manually?

Plugins, snippets, guidance on alternat methods of approach would be greatly appreciated.

EDIT WITH EXAMPLE:

I have a custom post type that basically acts like a page. When I create one, I want it to populate a custom taxonomy with its title. Daniel Dvorkin's answer is the sort of solution I was thinking about.

I have a custom post I am looking to categorize via a custom taxonomy. The taxonomy itself I want to be dependent on the items of another custom post type. Is it possible to populate a taxonomy via the items of a custom post type, or is it something I have to do manually?

Plugins, snippets, guidance on alternat methods of approach would be greatly appreciated.

EDIT WITH EXAMPLE:

I have a custom post type that basically acts like a page. When I create one, I want it to populate a custom taxonomy with its title. Daniel Dvorkin's answer is the sort of solution I was thinking about.

Share Improve this question edited May 26, 2011 at 8:14 stillmotion asked May 26, 2011 at 2:53 stillmotionstillmotion 1434 bronze badges 2
  • Manually i believe – xLRDxREVENGEx Commented May 26, 2011 at 3:16
  • try explaining the use case better – Bainternet Commented May 26, 2011 at 5:54
Add a comment  | 

3 Answers 3

Reset to default 4

I always use something like:

add_action('save_post', 'mdz_correlate_casos_taxonomy');
function mdz_correlate_casos_taxonomy( $post_id ){

    if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) 
        return $post_id;

    if ( 'YOUR CUSTOM POST TYPE' == $_POST['post_type'] ){
        if (!wp_is_post_revision($post_id)){
            if (!term_exists( $_POST["post_title"], 'YOUR CUSTOM TAXONOMY' )){

                $termid = wp_insert_term( $_POST["post_title"], 'YOUR CUSTOM TAXONOMY' );

            }
        }
    }

}

But this is prone to get inconsistent (ie: if you delete a post, the term won't get deleted)

To follow up on @MZAweb's answer. You can also delete the term automatically like so:

add_action( 'before_delete_post', 'cpk_delete_term' );
function cpk_delete_term( $post_id ) {

  $post = get_post( $post_id );

  if ( term_exists( $post->post_title, 'YOUR_TAXONOMY_NAME' ) ) {
    $term = get_term_by( 'name', $post->post_title, 'YOUR_TAXONOMY_NAME' );
    wp_delete_term( $term->term_id, 'YOUR_TAXONOMY_NAME' );
  }

}

Note we are not doing a check for the post type here because that isn't in the post object when trying to get that info (for whatever reason).

I adapted the code from MZAweb and codeprokanner and added lots more consistency checking to provide strict one-to-one relationships between custom posts and custom taxonomy terms. See this answer.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745103234a4611402.html

相关推荐

  • Populate Taxonomy from Custom Posts

    I have a custom post I am looking to categorize via a custom taxonomy. The taxonomy itself I want to be dependent on the

    12小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信