0

I'm trying to update and modify the accepted answer to this question Copy SEO Meta Desc "Custom Field" to Excerpt field? to use an ACF custom field and different "if" logic.

I want to check if the post has an ACF field called body; and if it does, then check to see if there is an existing _aioseop_description field for the post; if that is empty, write the $add_description to the _aioseop_description field.

But this doesn't work on plugin activation, and there are no errors in the debug.log.

Is my "if" logic correct?

register_activation_hook( __FILE__, 'wpse_activation_run' );

function wpse_activation_run()
{   
    $args = array( 
        'post_type'   => 'post',
        'numberposts' => -1,
        'post_status' => 'published',
    );
    $posts = get_posts( $args );

    $body = get_field('body');

    foreach ( $posts as $post )
    {

        if (!empty($body) )

        {

            $add_description = get_field('body'); substr($add_description, 0, 150);

            $aioseop = get_post_meta( $post->ID, '_aioseop_description' ,true);

                if (empty($aioseop) )
{

                $po['_aioseop_description'] = $add_description;
                wp_update_post($po);
            }
        }
    }   
}

Update: this works and is more of a "big hammer" approach as it doesn't do any checking as to if the _aioseop_description field is already populated or the ACF field exists; and this will overwrite the _aioseop_description field on every activation. This will work for a post and an ACF field called body. It also trims the output and strips html with wp_trim_words, and uses update_post_meta instead of updating the entire post.

register_activation_hook( __FILE__, 'wpse_activation_run' );

function wpse_activation_run()
{   
    $args = array( 
        'post_type'   => 'post',
       'numberposts' => -1,
       'post_status' => 'published',
    );

    $posts = get_posts( $args );

    foreach ( $posts as $post )

    {

    $add_description = get_field('body', $post->ID); 

    $trimmed = wp_trim_words( $add_description, $num_words = 20,'' );

    update_post_meta( $post->ID, '_aioseop_description', $trimmed );
}
0

2 things I noticed: get_field() isn't specifying a post to get the field from, which is necessary outside the loop:

$add_description = get_field( 'body', $post->ID );`

And to set a post meta value you should use update_post_meta(), not wp_update_post():

update_post_meta( $post->ID, '_aioseop_description', $add_description );
5
  • Thanks! Good points, and I can see why, but they don't make a difference. And still no errors in the log. And I removed the $body = get_field('body'); below get_posts in case that was being overwritten. – BlueDogRanch Oct 3 '18 at 16:46
  • Did you also remove the if (!empty($body) )? – Jacob Peattie Oct 3 '18 at 16:50
  • Ah, the if (!empty($body) ) seemed to be it; I guess that needs the $post->ID. So it works now, though I do want to include that condition to make it more efficient. I also found that the resulting descriptions contained html, so used wp_trim_words to strip those. Now I need to either overwrite all the descriptions that have html, or clean the descriptions in the database and run the plugin again. – BlueDogRanch Oct 3 '18 at 17:14
  • Well the problem was that you were checking it before the loop, so it wasn't doing anything. You should be checking the value of $add_description instead, after you set it. – Jacob Peattie Oct 3 '18 at 17:17
  • That makes sense... – BlueDogRanch Oct 3 '18 at 20:33

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