0

I'm working on a podcast website where the theme requires all podcast episode posts to be in a custom taxonomy called qtserie. The name of the particular qtserie taxonomy that these episodes should be under is called 'main_podcast'.

I have a plugin that automatically creates a post from the podcast RSS feed when an episode airs, but the plugin cannot assign custom taxonomies. How do I have the site automatically assign the qtserie taxonomy called 'podcast' to any new podcast post that is made by the plugin? I figure that I will have the plugin assign an arbitrary category to new posts, and will need to use in_category somehow, but I'm having trouble with the code.

This is the portion of the plugin code that ad

                    // Create the post
                    global $wpdb;
                    $post_id;
                    // Check if post already exists, if so - skip. First we'll look for the GUID, then at the title.
                    if(!empty($guid) && $guid != '') {
                        $query = "SELECT COUNT(*) FROM {$wpdb->postmeta} WHERE (meta_key = 'secondline_imported_guid' AND meta_value LIKE '%$guid%')";
                        $guid_count = intval($wpdb->get_var($query));
                    } else {
                        $guid_count = 0;
                    }
                    if($guid_count == 0) {

                        if( 0 === post_exists( $post_title, "", "", $secondline_import_post_type )) {

                            $post_id = wp_insert_post( $post );

                            // Continue if the import generate errors
                            if ( is_wp_error( $post_id ) ) {
                                continue;
                            }

                            // Add GUID for each post
                            add_post_meta( $post_id, 'secondline_imported_guid', $guid, true );

                            // Import Episode Number and Season Number
                            if ( function_exists('secondline_themes_theme_updater')) {
                                if (isset($episode_number) && $episode_number !== '') {
                                    add_post_meta( $post_id, 'secondline_themes_episode_number', $episode_number, true );
                                }
                                if (isset($season_number) && $season_number !== '') {
                                    add_post_meta( $post_id, 'secondline_themes_season_number', $season_number, true );
                                }
                            }

                            // Add episode categories
                            if( !empty($secondline_import_category) ) {
                                if( $secondline_import_post_type == 'podcast' ) {
                                    wp_set_post_terms( $post_id, $secondline_import_category, 'series', false );
                                } else {
                                    wp_set_post_terms( $post_id, $secondline_import_category, 'category', false );
                                }
                            }
````
3
  • You should be able to edit or extend your plugin to add the taxonomy. What plugin is it? Can you find the code where it creates the post?
    – Rup
    Commented Mar 16, 2021 at 17:57
  • It is the SecondLine Podcast Importer plugin. Commented Mar 16, 2021 at 18:10
  • Don't edit the plugin. Your edits will be removed when it upgrades. Better to write your own mini-plugin which relies on the other.
    – JakeParis
    Commented Mar 16, 2021 at 18:19

1 Answer 1

0

You'll want to hook onto the wp_insert_post action. Once the new post is created, you can then assign it to the taxonomy/term you wish.

It might look something like this:

add_action('wp_insert_post', function($postId, $post, $updatingExisting) {
    if( $updateExisting )
        return;

    // read up on these params at http://developer.wordpress.org/reference/functions/wp_set_post_terms/
    wp_set_post_terms( $postId, 'main_podcast', 'qtserie', true);
}, 10, 3);

Depending on what else is happening on your site, you might need to do some checking inside the filter to make sure you're only updating the posts you want to (this will run on creation of all posts, pages, custom-post-types, etc.

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.