0

Is there a way to set a default post_tag to all posts within a custom post type.

I would like all my posts from a custom posts type assigned to one tag displayed in the wp_tag_cloud. Is there a way to implement wp_set_object_terms( $object_id, $terms, $taxonomy, $append ) in the args for registering post type

Example from wp codex:

function codex_book_init() {
$labels = array(
    'name'               => _x( 'Books', 'post type general name', 'your-plugin-textdomain' ),
    'singular_name'      => _x( 'Book', 'post type singular name', 'your-plugin-textdomain' ),
    'menu_name'          => _x( 'Books', 'admin menu', 'your-plugin-textdomain' ),
    'name_admin_bar'     => _x( 'Book', 'add new on admin bar', 'your-plugin-textdomain' ),
    'add_new'            => _x( 'Add New', 'book', 'your-plugin-textdomain' ),
    'add_new_item'       => __( 'Add New Book', 'your-plugin-textdomain' ),
    'new_item'           => __( 'New Book', 'your-plugin-textdomain' ),
    'edit_item'          => __( 'Edit Book', 'your-plugin-textdomain' ),
    'view_item'          => __( 'View Book', 'your-plugin-textdomain' ),
    'all_items'          => __( 'All Books', 'your-plugin-textdomain' ),
    'search_items'       => __( 'Search Books', 'your-plugin-textdomain' ),
    'parent_item_colon'  => __( 'Parent Books:', 'your-plugin-textdomain' ),
    'not_found'          => __( 'No books found.', 'your-plugin-textdomain' ),
    'not_found_in_trash' => __( 'No books found in Trash.', 'your-plugin-textdomain' )
);

$args = array(
    'labels'             => $labels,
            'description'        => __( 'Description.', 'your-plugin-textdomain' ),
    'public'             => true,
    'publicly_queryable' => true,
    'show_ui'            => true,
    'show_in_menu'       => true,
    'query_var'          => true,
    'rewrite'            => array( 'slug' => 'book' ),
    'capability_type'    => 'post',
    'has_archive'        => true,
    'hierarchical'       => false,
    'menu_position'      => null,
    'supports'           => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )
);

register_post_type( 'book', $args );
}
1

To set the taxonomy post_tag to your custom post type, you need to set the taxonomies parameter in your arguments when registering your post type

taxonomies

(array) (optional) An array of registered taxonomies like category or post_tag that will be used with this post type. This can be used in lieu of calling register_taxonomy_for_object_type() directly. Custom taxonomies still need to be registered with register_taxonomy().

Default: no taxonomies

So you would add something like this in your arguments

'taxonomies' => ['post_tag'],

You will now have the tag meta box in your custom post type post page.

If you need to set a specific tag to all posts, you can run a custom query to get all the posts you need to alter, and then use wp_set_object_terms() on each post to set the specific tag

You can try the following: (Requires PHP 5.4+. Also, backup your database before running this query. You can run this once and remove it)

$args = [
    'post_type' => 'book',
    'posts_per_page' => -1, // If you need all posts, set according to needs
    'fields' => 'ids', // Only get post ID's, very lean query
    // Add any other paramaters according to need
];
$q = get_posts( $args );

foreach ( $q as $post ) {
    if (    term_exists( 'MY_TAG', 'post_tag' ) 
         && !has_tag( 'MY TAG', $post ) 
    ) // Only run if the tag is not yet assigned and if it exists
        wp_set_object_terms( 
            $post, // Post ID 
            'MY TAG', // Term to add, tag in this case 
            'post_tag', // Taxonomy the term belongs to
            true // Append the tag, don't replace tags
        );
}
1
  • Any feedback on this issue? – Pieter Goosen Mar 1 '16 at 15:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.