Elliot! you’re a hero!
Stupid of me not to think about debugging first! You were right on point about that $target_product
was not a single, but an array of post objects!
so using this updated variable $review_target_product = $target_product['0']->ID;
made my code work!
For anyone interested in the future, this is all the code I used to construct my post titles in my functions.php
//Auto add and update Title field:
function my_post_title_updater( $post_id ) {
$my_post = array();
$my_post['ID'] = $post_id;
$manufacturer = get_field('manufacturer');
$target_product = get_field('target_product');
$review_target_product = $target_product['0']->ID;
$manufacturer_target = get_field('manufacturer', $review_target_product);
if ( get_post_type() == 'manufacturer' ) {
$my_post['post_title'] = get_field('manufacturer_name');
} elseif ( get_post_type() == 'products' ) {
$my_post['post_title'] = get_field('kitName') . ' (' . get_field('manufacturer_name', $manufacturer->ID) . ' ' . get_field('kitNumber') . ')';
} elseif ( get_post_type() == 'reviews' ) {
$my_post['post_title'] = get_field('kitName', $review_target_product) . ' (' . get_field('manufacturer_name', $manufacturer_target) . ' ' . get_field('kitNumber', $review_target_product) . ')';
}
// Update the post into the database
wp_update_post( $my_post );
}
// run after ACF saves the $_POST['fields'] data
add_action('acf/save_post', 'my_post_title_updater', 20);
haha true! that did the trick.
Stupid that I didn’t even try your code at first!
Thanks anyway! p.s. I would really appreciate if you give your experienced view on my question here
Thanks Jonathan!
Okay, Now I have this and it still does not work, what’s going on?
//Auto assign Featured image from 'post_image'
function acf_set_featured_image( $value, $post_id, $field ){
if($value != ''){
//Add the value which is the image ID to the _thumbnail_id meta data for the current post
update_post_meta($post_id, '_thumbnail_id', $value->ID);
}
return $value;
}
add_filter('acf/update_value/key=field_53b58efe0accf', 'acf_set_featured_image', 10, 3);
Strange right? I tested it with field output->ID and just using $value
that works great… but I need the object
Thanks both of you for your replies! it’s stange to me, I did a search for _thumbnail_id
but did not find any documentation on it by WordPress so I thought it was something you created!
Thanks for the heads-up on the $value->ID
@Jonathan!
I want my post thumbnail to update on existing posts as well as to add on new posts so I went to update_post_meta
and removed the if
statement, now I have this:
//Auto assign Featured image from 'post_image'
function acf_set_featured_image( $value, $post_id, $field ){
update_post_meta($post_id, '_thumbnail_id', $value->ID);
return $value;
}
add_filter('acf/update_value/name=post_image', 'acf_set_featured_image', 10, 3);
which is not working…. What am I missing? thanks again!
@Jonathan, Today I started learning about hooks because I did not understand your given code earlier today. Now I understand everything except the add_post_meta($post_id, '_thumbnail_id', $value);
where does '_thumbnail_id'
come from?
Im trying to get the same thing to work, but I have an image field thats outputs as an object for other reasons. How can I make this code work with an image object field?
Thanks!
Thank you! However, I see no option to set the ‘post object’ field type to return a post ID value… there is no option at all for the return value
Now when I put this in my review template inside the loop:
<?php $mf_post_id = get_field('manufacturer');
if ($mf_post_id) {
$image = get_field('manufacturer_logo', $mf_post_id)
}
?>
the page does not load anymore, meaning there’s an error.
What should I do?
I do not get it, Im sorry.
Here my situation clearly explained:
I have a custom post type called ‘manufacturers’ this custom post type has a custom field called ‘manufacturer_logo’, this is an image field that returns the image url.
Now in my other custom post type, called ‘reviews’ I created a custom field called ‘manufacturer’ this is a ‘post object’ field type that has post type set at ‘manufacturers’
When I add a review and select a manufacturer, I need the php code that pulls the ‘manufacturer_logo’ field from the selected post in the ‘manufacturers’ field and echoes the image url.
for example, I add a review and select ‘Apple’ as the manufacturer, Apple is a custom post and contains a image in the ‘manufacturer_logo’ field, I need the url of that image to be echoed on the review page.
You get it? Thanks for trying to help! really appreciate it!
Thanks! setting this up right now but I can’t seem to display the post object as the “manufacturer_logo” fields of the manufacturers post type…
How do I make the post object relational field output as an image?
Thanks!
thanks!
I now put this on one line in order to echo a part of an url:
<?php $term = get_field('manufacturer'); if( $term ): ?><?php echo $term->name; ?><?php endif; ?>
Is there any way to shorten the code any further? I just need the taxonomy name.
for example, would it be a problem to pull the “if” statement out?
Thanks a lot!
Thanks for the reply! that works great, however, im unable to print the custom taxonomy name, how can I achieve this in my template?
right now I have this: <?php the_field('manufacturer'); ?>
and it will print the cat id
Thank you for your help!
I was thinking of an if statement as well, but know that the selection field might grow to a lot of options, like 50+ easily. This means I will get a huge if statement, wouldn’t that be a problem?
Ah wait, I just found an option. If I name all my pictures “logo-brandname.jpeg” I can make a map in my /images folder with brand logos. then I can just upload the pictures to that map, e.g. “logo-coke.jpeg”, “logo-apple.jpeg”, “logo-sony.jpeg”
If I then make sure that the select field options match the brand name in the filename I can just use <img src="/images/brand/logo-<?php the_field('reviewBrand'); ?>.jpeg">
right? that would be very convenient!
Thanks for helping me on this!
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.