admin管理员组

文章数量:1122846

I have a plugin that adds a CPT, but it does not support Tags (normal post tags), which I need it to.

SO what I need is a code snippet to add to my Child Theme's functions.php so that as the plugin gets updated my code is not overwritten.

I am not looking to create a new taxonomy, I simply want to associate normal Posts and CPT posts with the same Post Tag.

I have a plugin that adds a CPT, but it does not support Tags (normal post tags), which I need it to.

SO what I need is a code snippet to add to my Child Theme's functions.php so that as the plugin gets updated my code is not overwritten.

I am not looking to create a new taxonomy, I simply want to associate normal Posts and CPT posts with the same Post Tag.

Share Improve this question asked Mar 15, 2023 at 19:48 TrishaTrisha 4282 silver badges11 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 2

You can use the register_taxonomy_for_object_type() function to associate the default post tags taxonomy with your custom post type.

Here's an example code snippet that you can add to your Child Theme's functions.php. Replace "your_custom_post_type" with the name of your custom post type.

function associate_tags_with_cpt() {
    register_taxonomy_for_object_type('post_tag', 'your_custom_post_type');
}
add_action('init', 'associate_tags_with_cpt');

https://developer.wordpress.org/reference/functions/register_taxonomy_for_object_type/

I marked Yurix' answer as helpful although it did not solve my problem, because I did find that it sent me off in the right direction, even if it wasn't the right solution.

I found in the plugin's code that registered the CPT the use of $defaults to set up all the $args for the CPT and it's custom taxonomy (to which I wanted to add post_tags) so while the code above didn't work, by adding code to add to the $defaults, this did work:

    // Adds support for normal Post Tags to FAQs
    function my_faq_defaults( $defaults ) {
    $defaults['post_type']['args']['taxonomies'] = array('post_tag');
    return $defaults;
    }
    add_filter( 'arconix_faq_defaults', 'my_faq_defaults' );

I really don't know exactly why Yurix' code did not work, it looks like it should have (another reason I gave it a 'helpful' vote), but I assume that it has to do with how the plugin's registers its CPT, using a variable called $defaults to contain all of its arguments, where 'taxonomies' was not mentioned (since it also sets up its own custom taxonomy).....so I just added 'taxonomies' to $defaults......

本文标签: Adding support for Post Tags to a CPT added by a plugin