Ask your WordPress questions! Pay money and get answers fast! (more info)

How do I copy the value of a taxnomy to an advanced custom field? WordPress

  • SOLVED

I have a custom post type labelled "SKU", which has a taxonomy of "producer" for each post.

I also have a ACF text field of "producer_name" for the post type "SKU"

currently the ACF text field is empty and Id like to copy the value of "producer" into the field "producer_name" for each SKU post.

Answers (3)

2015-05-30

Andrea P answers:

hello,

if I understood correctly your situation, you just need to use a taxonomy field rather than a text field.

you then select the taxonomy producer, and ensure to tick the checkbox "Load value based on the post's terms and update the post's terms on save"

then in each SKU post, you will have the chance to select which producer to select, and automatically you will also have the taxonomy added to the post.

cheers



*** EDITED ***
The above was actually not correct. look at the following messages posted by me. the reason because this was selected as correct answer, along with the other one, is because I have then provided a workaround which allow to use a tweaked version of Arnav's function, without bumping on a php script size error.


dataharvest comments:

just to be clear - the values i want are contained in a taxonomy of "producer" for a custom post type SKU - and i want to copy those values (text ) to an ACF field named "producer_sorting" which is currently empty.


Andrea P comments:

ah ok, so my suggestion is not what you needed, sorry.

I think then the solution for you is the one suggested by Arnav.

even if the number seems high, those are 36Mb and they are not that much (actually is the very minium setup for PHP scripts. you should increase that limit at least to 64Mb.

you could be able to do it by simply changing setting on wordpress config.php, but probably you'll have to contact your host, because when the php limit is so small, it means that your hosting provider is not quite generous and so I doubt that they will actually allow to increase that value by wordpress end.

more info here:
http://codex.wordpress.org/Editing_wp-config.php#Increasing_memory_allocated_to_PHP

cheers


Andrea P comments:

in any case, that function can be run only once (if it is not stopped by the php script limit), and then it will update all the fields and you'll take care of manually fill the new post's fields.
so after the first run, you can delete that function. hence, it doesn't really matter how many resources it will consume to complete its loop. and in any case, 36Mb are a very low limit. you are probably consuming it all just with wp core and plugins which run for each page load.

raise your php script limit to 64Mb and your problem will be sorted with Arnav function. ;)


dataharvest comments:

as per my host:

script limit is at 128, max is 256 and currently that script is hitting 367MB - changed "fastcgi" to "cgi" as recommended by hosting company - dreamhost.

would it be possible to write this script as a "while" loop so that it does the work one record at a time? would it be possible to run a mysql script directly at the DB?

thanks so much for your help.

GG


Andrea P comments:

Ah! I am sorry then, I've seen a digit less and got tricked because many host offers only 32Mb as default.

I'm not sure if a while loop would significantly reduce it, but you could try using wp query and a while loop


$args = array(
'post_type' => 'sku',
'posts_per_page' => -1
);
$my_query = new WP_Query($args);

if ( $my_query->have_posts() ):

while ( $my_query->have_posts() ) : $my_query->the_post();

$post_id = get_the_ID();
$term_list = wp_get_post_terms($post_id, 'producer', array("fields" => "names"));

if( $term_list ){

$term_name = $term_list[0];
update_post_meta( $post_id , 'producer_name', $term_name );

}

endwhile;

endif;


Andrea P comments:

actually, as you have to run the function only to loop all the posts once, you could manually run it a few time, doing a bit of posts at time. by instance the first time you set the query args like thins


$args = array(
'post_type' => 'sku',
'posts_per_page' => 300
);


then you'll load a page to run it, and that will update the last 300 posts, so you edit the function and change the args like


$args = array(
'post_type' => 'sku',
'posts_per_page' => 300,
'offset' => 300
);


load another time the page and then change the function to


$args = array(
'post_type' => 'sku',
'posts_per_page' => 300,
'offset' => 600
);



etc..


dataharvest comments:

great idea. but for some reason when I limit the number of posts I get the following error:

Fatal error: Cannot use object of type WP_Error as array in /home/dataharv/skurnik.dataharvest.net/wp-content/themes/skurnik/functions.php on line 69

using:

$args = array(

'post_type' => 'SKU',

'posts_per_page' => 5

);

$my_query = new WP_Query($args);


if ( $my_query->have_posts() ):

while ( $my_query->have_posts() ) : $my_query->the_post();

$post_id = get_the_ID();

$term_list = wp_get_post_terms($post_id, 'producer_name', array("fields" => "names"));

if( $term_list ){

$term_name = $term_list[0];

update_post_meta( $post_id , 'producer_sorting', $term_name );

}

endwhile;

endif;


Andrea P comments:

sorry, I must have wrote something wrong within the code..
which is the line 69 on that page?

it might be because I've tried to retrieve only the term names but I misunderstood what it gives in return..

try substituting this


$term_list = wp_get_post_terms($post_id, 'producer_name', array("fields" => "names"));
if( $term_list ){
$term_name = $term_list[0];
update_post_meta( $post_id , 'producer_sorting', $term_name );
}



with this


$term_list = wp_get_post_terms($post_id, 'producer', array("fields" => "all"));
if( $term_list ){
$term_name = $term_list[0]->name;

update_post_meta( $post_id , 'producer_name', $term_name );
}


Andrea P comments:

sorry, is
'producer_sorting'

and not 'producer_name'

I've copied and pasted the other one ;)


dataharvest comments:

thanks!

still same error.

Fatal error: Cannot use object of type WP_Error as array in /home/dataharv/skurnik.dataharvest.net/wp-content/themes/skurnik/header.php on line 59

line 59:

$term_name = $term_list[0]->name;



<strong>this is what I'm using:</strong>

$args = array(
'post_type' => 'SKU',
'posts_per_page' =>100
);

$my_query = new WP_Query($args);

if ( $my_query->have_posts() ):

while ( $my_query->have_posts() ) : $my_query->the_post();

$post_id = get_the_ID();

$term_list = wp_get_post_terms($post_id, 'producer', array("fields" => "all"));

if( $term_list ){

$term_name = $term_list[0]->name;

update_post_meta( $post_id , 'producer_sorting', $term_name );

}

endwhile;

endif;


Andrea P comments:

ok, I did a couple of test and finally I understood what is returned by the wp_get_post_terms when getting only the field "name"

you can substitute that piece of code with this:


$term_list = wp_get_post_terms($post_id, 'producer', array("fields" => "names"));
if( $term_list ){
foreach ($term_list as $term_name){
update_post_meta( $post_id , 'producer_sorting', $term_name );
}
}

2015-05-30

Arnav Joy answers:

try this code and write it in top of header.php file or in functions.php of your theme

<?php

$posts = get_posts('post_type=sku&posts_per_page=-1');
if( $posts ){
foreach( $posts as $post_data ){
$post_id = $post_data->ID;
$term_list = wp_get_post_terms($post_id, 'producer', array("fields" => "all"));
if( $term_list ){
$term_name = $term_list[0]->name;

update_post_meta( $post_id , 'producer_name', $term_name );
}
}
}


dataharvest comments:

thank you for your help - I'm getting this error:

Fatal error: Allowed memory size of 367001600 bytes exhausted (tried to allocate 93 bytes) in /home/dataharv/skurnik.dataharvest.net/wp-includes/meta.php on line 826

just to be clear - the values i want are contained in a taxonomy of "producer" for a custom post type SKU - and i want to copy those values (text ) to an ACF field named "producer_sorting"

as you see its got a large memory allocation - maybe its better if done as a "while" loop or directly thru SQL

thank you


Arnav Joy comments:

how many posts are there?


dataharvest comments:

there are 4000 records that need to be adjusted....

2015-06-02

Woo Gang answers:

You can use Custom Content Shortcode plugin to fetch anything from taxonomy.
Just give it try