Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialBoris Kamp
16,660 Pointsin_array() not passing in function
Hi guys!
I have this code:
//Auto add and update Title field:
function my_post_title_updater( $post_id ) {
$my_post = array();
$my_post['ID'] = $post_id;
$posttypes = array( 'page', 'post', 'portfolio' );
$currentposttype = get_post_type();
if ( in_array( $currentposttype, $posttypes ) ) {
$my_post['post_title'] = get_field('kop');
}
//Unhook function to prevent infitnite looping
remove_filter('acf/save_post', 'my_post_title_updater', 1);
// Update the post into the database
wp_update_post( $my_post );
//Rehook function to prevent infitnite looping
add_filter('acf/save_post', 'my_post_title_updater', 20, 3);
}
// run after ACF saves the $_POST['fields'] data
add_action('acf/save_post', 'my_post_title_updater', 20);
in order to update the post title of a post type with an ACF field.
the error is in this part : in_array( $currentposttype, $posttypes )
this is not working.
doing this works but is less clean code in my opinion:
if ( get_post_type() == 'page' || get_post_type() == 'post' || get_post_type() == 'portfolio') {
$my_post['post_title'] = get_field('kop');
}
How can I solve this?
1 Answer
Gianmarco Mazzoran
22,076 PointsHi boris kamp,
Are you working with wordpress? If yes, I think you have to pass the parameters inside the get_post_type() function like this:
<?php if ( get_post_type('page') || get_post_type('post') || get_post_type('portfolio'); ) ?>
$my_post['post_title'] = get_field('kop');
<?php endif; ?>
i'm not sure if this works for you since i've only complete few courses about wordpress :)
Boris Kamp
16,660 PointsBoris Kamp
16,660 PointsHi Gianmarco!
Thanks for helping, you're talking about the part that actually is working. My first bit of code is not working although it should be. I did further debugging and found it is working, except when post_type is
page
.Gianmarco Mazzoran
22,076 PointsGianmarco Mazzoran
22,076 PointsOps! my mistake sorry!
Well if other post_type is working maybe the problem is in the "page" template. Otherwise, logically, "portfolio" and "post" should not work.
Boris Kamp
16,660 PointsBoris Kamp
16,660 PointsNo problem!
Yeah, that's the mystery! I have no clue on this, I started on a clean install. I have no idea why it's not working
Gianmarco Mazzoran
22,076 PointsGianmarco Mazzoran
22,076 PointsI'm sure that you can solve the mistery! ;) in the meantime it's better that I go back to study! :)
Happy coding!