admin管理员组

文章数量:1301490

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 );
                                }
                            }
````

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 );
                                }
                            }
````
Share Improve this question edited Mar 16, 2021 at 18:11 user1323153 asked Mar 16, 2021 at 17:03 user1323153user1323153 11 bronze badge 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. – user1323153 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
Add a comment  | 

1 Answer 1

Reset to default 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/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.

本文标签: categoriesAutomatically add custom taxonomy to posts in a category