User meta to post

时间:2017-07-13 作者:Andrea A

我在用户配置文件中创建了一个字段(不是acf)。该字段名为“genere”,我在用ACF创建的帖子中有另一个字段名为相同的“genere”,但它在我的函数中由$field_key.

我现在可以通过了usermeta 字段到作者的帖子到acf_field 具有acf_update_field 但只有在我点击“编辑用户”中的“更新用户”之后。php。

每个作者只有一篇文章,最多两篇是错误的。所以当我在“用户编辑”中单击“更新用户”时。php,作者帖子也更新了。我想要每一个usermeta “genere”字段值自动传递给作者的帖子already created.

另一个my previous函数在创建此用户时创建作者的帖子。当我更新用户时,他的帖子已经存在。因此,对于未来作者的帖子,我不需要解决方案,因为通过更新概要文件,字段值可以很好地传递并工作。The problem is only for the posts already created (默认情况下,在我的主题中,每个用户都有一篇帖子)。

有2500个用户,我需要在不手动更新用户编辑的情况下将值传递给帖子。php页。

这是我在文件函数中的代码。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\' );
可能是行动user_registerpersonal_options_update 根据hwl在回答中的建议,我找到了这个工作解决方案。

批量操作>从MySql中通过usermeta获取用户>使用foreach获取用户id>使用foreach post更新Posteta

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; 
  }
致以最诚挚的问候

1 个回复
最合适的回答,由SO网友:hwl 整理而成

我认为至少有一种方法是以下过程:-按作者获取所有帖子-foreach post:-获取作者user_meta genere - 更新post_meta 具有该值

我有not 测试了下面的任何代码,所以我不会复制/粘贴。但希望通读它能让你朝着正确的方向前进。

最后,我链接了有关使用过的函数/类的信息。

<小时>

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\' );

结束

相关推荐