2

I am trying to add custom taxonomy (tags) to WordPress default post type. because I wanna use that taxonomy for custom post type as well.

// Register Custom Taxonomy
function tag_synchro() {

$labels = array(
    'name'                       => _x( 'QA Tags', 'Taxonomy General Name', ET_DOMAIN ),
    'singular_name'              => _x( 'QA Tag', 'Taxonomy Singular Name', ET_DOMAIN ),
    'menu_name'                  => __( 'QA Tags', ET_DOMAIN ),
    'all_items'                  => __( 'All Tags', ET_DOMAIN ),
    'parent_item'                => __( 'Parent Item', ET_DOMAIN ),
    'parent_item_colon'          => __( 'Parent Item:', ET_DOMAIN ),
    'new_item_name'              => __( 'New Tags Name', ET_DOMAIN ),
    'add_new_item'               => __( 'Add New tag', ET_DOMAIN ),
    'edit_item'                  => __( 'Edit tag', ET_DOMAIN ),
    'update_item'                => __( 'Update tag', ET_DOMAIN ),
    'separate_items_with_commas' => __( 'Separate tags with commas', ET_DOMAIN ),
    'search_items'               => __( 'Search Items', ET_DOMAIN ),
    'add_or_remove_items'        => __( 'Add or remove items', ET_DOMAIN ),
    'choose_from_most_used'      => __( 'Choose from the most used tagd', ET_DOMAIN ),
    'not_found'                  => __( 'Not Found', ET_DOMAIN ),
);
$args = array(
    'labels'                     => $labels,
    'hierarchical'               => false,
    'public'                     => true,
    'show_ui'                    => true,
    'show_admin_column'          => true,
    'show_in_nav_menus'          => true,
    'show_tagcloud'              => true,
    'update_count_callback'      => '_update_post_term_count',
    'query_var'             => true,
    'rewrite'               => array( 'slug' => ae_get_option('tag_slug', 'qa-tag') ),
    );
register_taxonomy( 'qa_tag', array( 'post' ), $args );

}

// Hook into the 'init' action
add_action( 'init', 'tag_synchro', 0 );

I used above code, and it's seems not working. is there any way I can accomplish this work. Thank you in advance

3 Answers 3

1

Try this

add_action( 'init', 'tag_synchro', 10 );

It might be the case that, when you set priority to 0 , your callback does not get called.

0
3

The correct way to register a taxonomy is to wait for the action registered_post_type, because that’s the only way to know that your post type actually exists. This action is fired when a post type was registered.

Core post types are registered twice for reasons. You have to wait for the second call, because the rewrite object is not available earlier. If your taxonomy should have permalinks, you get an error when you register it on the first call.

Affected built-in post types are:

  • post
  • page
  • attachment
  • revision
  • nav_menu_item

Your code should be rewritten to something like this:

add_action( 'registered_post_type', function( $post_type ) {

    static $first_call = TRUE;

    if ( 'post' !== $post_type || $first_call ) {
        $first_call = FALSE;
        return;
    }

    register_taxonomy(
        'test',
        'post',
        [
            'labels' => [
                'name' => 'Tests',
                'singular_name' => 'Test'
            ]
        ]
    );

    $first_call = FALSE;
});
2
  • Hello, This is also, good, but if the custom post type is already registered what we can do ?
    – dazunE
    Aug 28, 2014 at 14:14
  • @dazunE Register the callback early enough.
    – fuxia
    Aug 28, 2014 at 14:24
0

From the register_taxonomy() codex page:

Better be safe than sorry when registering custom taxonomies for custom post types. Use register_taxonomy_for_object_type() right after the function to interconnect them. Else you could run into minetraps where the post type isn't attached inside filter callback that run during parse_request or pre_get_posts.

So you might want to actually do it, like this:

// for the buil-in post type post  
register_taxonomy_for_object_type( 'qa_tag', 'post' );  
// for your custom post type  
register_taxonomy_for_object_type( 'qa_tag', 'your_cpt' );

Additionally there is the possibility to use the taxonomies argument when using register_post_type() to register your taxonomy with your custom post type, but:

Even if you register a taxonomy while creating the post type, you must still explicitly register and define the taxonomy using register_taxonomy().

Nonetheless it can replace above suggested register_taxonomy_for_object_type() call.

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.