0

I have an additional field "year", I want to transfer the data from it in the tags for all existing posts, that it was possible use the sorting by year. How best to do this. Maybe there is a ready plugin?

1

You don't need a plugin, this will a one-time thing you need to do.

You need to write a script to do this, run it, and then you can get rid of the script.

Create a new PHP file in your theme's root and call it page-transfer-tag-info.php

Put this code into the new file:

<?php

// first, get all the posts you want to do this for
$posts = get_posts(array('post_type'=>'the_required_post_type', 'posts_per_page'=>-1);

foreach($posts as $post){

    $terms = get_the_terms($post->ID, 'the_taxonomy_in_question'); // I'm guessing this would be 'tag' in your case

    foreach($terms as $term){

        update_post_meta($post->ID, 'year', $term->name); // assuming the meta_key of your custom field is 'year'

    }

}

To run the script, create a page (in admin) called 'transfer tag info' and then visit the page in your browser.

Now you can delete the page and the file.

All those posts will now have their 'year' set to the value of the tag's name.

So a post that had a tag '2016' will now have it's custom field (post meta) 'year' set to '2016'.

EDIT (& DISCLAIMER):

Back up your DB before running this script, in case something goes wrong!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.