0

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.

2 Answers 2

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/

3
  • Thank you Yurix, I deeply appreciate your willingness to contribute and your quick reply, however this answer did not work for me .....I did figure out a different solution by combing thru the Plugin to see how they registered the CPT in the first place, and added to my functions some code that did work, I'll paste that in a new answer below.
    – Trisha
    Commented Mar 15, 2023 at 21:16
  • 1
    Hi Trisha, I tested the snippet in a child theme under exactly the same conditions. The 3rd party plugin creates a CPT inside some_plugin_custom_post_init function, with a hook in the init action add_action( 'init', 'some_plugin_custom_post_init' ); Inside the same function it registers its own custom taxonomy for the CPT. In my tests, the snippet works perfectly, in addition to a custom taxonomy, CPT gets full post tag functionality (tags can be created, assigned, saved). Anyway, glad you found a solution.
    – Yurix
    Commented Mar 16, 2023 at 1:24
  • Thank you Yurix, I do know that very often there are many factors to consider when it comes to why code will work, or not......other plugins, core version, etc. Thank you again for being a member here, I really appreciate the coders like you!
    – Trisha
    Commented Mar 16, 2023 at 21:32
0

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......

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.