admin管理员组

文章数量:1122832

I'm working on a site update for a client. The current version of their site stores a lot of information within custom post meta fields, added as plain text boxes using Advanced Custom Fields. We think this information would serve the site better if it existed as a taxonomy, so we'd like to move all this information into a custom taxonomy.

This is a film site and one example is that the film's director is stored as a plain text post meta field. If it were part of a Director taxonomy instead, we could easily look at all films tagged with that Director.

So my question - is there is an easy way to move or copy information in a post meta field to a custom taxonomy? My plan at the moment is to look at a plugin for copying info between taxonomies and seeing if I can modify that. But if anyone knows of another solution, I'd be grateful!

Many thanks.

I'm working on a site update for a client. The current version of their site stores a lot of information within custom post meta fields, added as plain text boxes using Advanced Custom Fields. We think this information would serve the site better if it existed as a taxonomy, so we'd like to move all this information into a custom taxonomy.

This is a film site and one example is that the film's director is stored as a plain text post meta field. If it were part of a Director taxonomy instead, we could easily look at all films tagged with that Director.

So my question - is there is an easy way to move or copy information in a post meta field to a custom taxonomy? My plan at the moment is to look at a plugin for copying info between taxonomies and seeing if I can modify that. But if anyone knows of another solution, I'd be grateful!

Many thanks.

Share Improve this question asked Aug 13, 2020 at 19:31 KateKate 212 bronze badges 1
  • 1 There's no easy way. Your best bet would be to write a WP-CLI script to loop through the posts and assign the new terms to your custom taxonomy as you go. – vancoder Commented Aug 13, 2020 at 21:02
Add a comment  | 

2 Answers 2

Reset to default 0

You will need to write some code for this I think. Start creating the new taxonomies and the query the right custom field to get the list of values. Use wp_insert_term() to add the new term. Then use wp_set_post_terms() to assign the terms to each post.

I just had to do this, so I'm sharing the code I wrote to move the author name for a book from post meta to its custom taxonomy.

Maybe is not perfect but it does the job. I've placed this code in functions.php. You just have to add set_author=1 to the url query params.

add_action( 'current_screen', function( $screen ) {
    if (
        'edit-wse_book' === $screen->id
        && isset( $_GET['set_author'] )
        && '1' === $_GET['set_author']
    ) {
        $books = get_posts( [
            'numberposts' => -1,
            'post_type' => 'wse_book',
        ] );

        foreach ( $books as $book ) {
            if ( empty( $book->author_name ) ) {
                continue;
            }
            echo "Processing book:\t" . $book->ID . "\t\tAuthor:\t" . $book->author_name . PHP_EOL;

            $author = term_exists( $book->author_name, 'wse_book_author' );

            if ( is_null( $author ) ) {
                $new_term = wp_insert_term( $book->author_name, 'wse_book_author', [ 'slug' => sanitize_title( $book->author_name ) ] );

                if ( is_array( $new_term ) ) {
                    wp_set_post_terms( $book->ID, [ (int) $new_term['term_id'] ], 'wse_book_author' );
                }
            } else {
                wp_set_post_terms( $book->ID, [ (int) $author['term_id'] ], 'wse_book_author' );
            }
        }
    }
} );

本文标签: Convert post meta to custom taxonomy