3

I created a field (not acf) in the user profile. The field is called “genere”, and I have another field in the post created with ACF called the same "genere", but it is invoked in my function by the $field_key.

I can now pass this usermeta field to the author’s post to a acf_field with acf_update_field but only after I click on “Update User” in edit-user.php.

There is only one post per author, at most two by mistake. So when I click on "Update User" in user-edit.php, the author post also updates. I would like every usermeta of "genere" field value to pass automatically to the author's post already created.

Another my previous function creates the author's post when this user is created. When I update the user, his post already exists. So for future author's posts I do not need solutions because by updating the profile, the field value passes well and works. The problem is only for the posts already created (in my theme by default each user has a post).

There are 2500 users and I would need to pass the value to the post without manually updating the user-edit.php page.

This is my code in the file functions.php

function save_acf($user_id)
  {
    global $post;
    $args = array(
   'author' =>  $user_id,
    );
   $myposts = get_posts( $args );
   foreach( $myposts as $post ) :
    setup_postdata($post);
    $numero = $post->ID;
   endforeach; 

 if( isset($_POST['genere']) ) { 
    $post_id = $numero ;
    $field_key = "field_5941b968b5ad6";
    $value =  sanitize_text_field( $_POST['genere'] ) ;
  update_field( $field_key, $value, $post_id );}
}

add_action( 'user_register', 'save_acf');
add_action( 'personal_options_update', 'save_acf' );
add_action( 'edit_user_profile_update', 'save_acf' );
  • Probably the actions user_register and personal_options_update are no influence

EDIT

With hwl's suggestions in his answer I found this work solution.

Bulk action > get user by usermeta from MySql > get the id of users with foreach > update postmeta with foreach post

add_filter( 'bulk_actions-edit-post', 'register_my_bulk_actions2' );
 function register_my_bulk_actions2($post_ids) {
    global $wpdb;
      $users = $wpdb->get_results( "SELECT user_id FROM $wpdb->usermeta WHERE meta_key = 'genere'AND meta_value = 'Maschio' " );
    foreach ( $users as $a ) {
        $author_ids[] = $a->user_id;
    }

   $args = array( 'posts_per_page' => -1 ,  'author__in' =>  $author_ids, );
   $myposts = get_posts( $args );
   foreach( $myposts as $post ) :
     $numero = $post->ID;
     update_post_meta( $numero, 'genere', 'Maschio' );
   endforeach; 
  }

Best regards

2
  • Can you clarify the problem you're trying to solve? You have a user_meta of genere, and you want to save the value of that in the acf_field of all of the posts by that author, for every author? For current posts and/or for new posts as they are added?
    – hwl
    Jul 13, 2017 at 3:04
  • Dear hwl thank you for you attention again. I have updated the question with a bit more information.
    – Andrea A
    Jul 13, 2017 at 8:23

1 Answer 1

2

I think at leasts one approach to this would be the following process: - get all posts by authors - foreach post: - get the author user_meta genere - update the post_meta with that value

I have not tested any of the code below, so I wouldn't copy/paste. But hopefully reading through it will get you in the right direction.

I've linked to info on used functions/classes at end.


function update_post_meta_with_user_meta() {
         //setup arguments to only get users with author role
         $user_args = array( 
                          'role' => 'author', 
                          );
        $authors   =  get_users( $user_args );
        //the below foreach could be replaced by adding something like:
        // fields => array( 'ID' ) to $user_args

       //instead I am just going through returned array of WP_User objects 
       //  and putting IDs into array
        foreach ( $authors as $a ) {
            $author_ids[] = $a->ID;
        }

       //setup post query arguments to only give us posts with authors in author_id array
       //which means only posts that have an author with the WP role of author
       // should exclude Editors, Admins, etc. that maybe have authored posts
        $post_args = array(
                          'author__in' => $author_ids,
                        );

        //a new WP_Query with these args   
        $post_query   = new WP_Query( $post_args ) ) );

        //make sure we have posts returned
        if ( $post_query->have_posts() ) {

            //loop
            while ( $post_query->have_posts() ) {

                $post_query->the_post();

                //set $post_id variable to current post
                $post_id = get_the_id();

                //get author meta for author of current post
                $author_genere = get_the_author_meta('genere');

                //update the meta of the current post (by ID) 
                //  with the value of its author's user meta key
                update_post_meta( $post_id, 'genere', $author_genere );
            }

            //reset the postdata
            wp_reset_postdata();
        }

    }
    //hook the above to init
    add_action( 'init', 'update_post_meta_with_user_meta' );

5
  • I have tried but it doesn't work...
    – Andrea A
    Jul 13, 2017 at 14:28
  • Have tried what?
    – hwl
    Jul 13, 2017 at 14:30
  • All your functions, coping and pasting. There was a syntax error here: $post_query = new WP_Query( $post_args ) ) ); I'll check later your code. Anyway... thank you. I hope soon to found a solution :)
    – Andrea A
    Jul 13, 2017 at 14:34
  • This is a walk through not complete code. As I stated, I wouldn't copy/paste. But hopefully reading through it will get you in the right direction. All those links give greater detail to the Wordpress specific pieces. Avoid asking for completed code, as moderators could close your question as off topic under asking to work for me for free. If you don't follow some part of the logic, do ask about that. There could easily be a more efficient way. :)
    – hwl
    Jul 13, 2017 at 16:30
  • Yes you are right. But I didn't have time and I made quick tests. Now I got inspiration from your code but I have not found the solution yet. Maybe the solution by MySql is the simplest. Anyway, thank you very much,
    – Andrea A
    Jul 13, 2017 at 21:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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