Can’t get custom user meta data in ‘user_register’ hook after WP AllImport.

[ad_1]

I have WP All Import set up to create a new user with a bunch of custom fields.

In functions.php I am using `add_action(‘user_register’, ‘my_function’, 10, 1);` to create a custom post. I want the custom post to display some of the data found in the custom fields of the user.

If I view all users and click on the newly created user I can see the custom field `stand_number: ‘D180’;` which is great. But in my\_function, if I use `$stand_number = get_user_meta($user_id, ‘stand_number’)` it returns empty.

Ideally I want to be doing `add_post_meta($post_id, ‘stand_number’, $stand_number)` but it doesn’t work.

I’ve also tried `add_post_meta($post_id, ‘test’, $user_id)` just to get all the meta values of the user, but the custom ones have never showed up.

Any help would be appreciated thank you.

[ad_2]
3 Comments
  1. It looks like the issue might be related to the order in which the hooks are firing. WP All Import might still be in the process of importing and updating the user meta when your ‘user_register’ hook fires, resulting in an empty value for the custom field ‘stand_number’.

    To fix this issue, you can try changing the priority of your hook to a higher value, so that it runs after WP All Import has finished its processing. You can change the priority value in the add_action function. For example, try changing the priority from 10 to 100:

    **add_action(‘user_register’, ‘my_function’, 100, 1);**

    If that doesn’t work, you can try using a different action hook provided by WP All Import, such as ‘pmxi_saved_post’. This hook is triggered after a post has been imported or updated. In this case, you need to modify your function to get the user associated with the imported post, and then get the user meta.
    This should ensure that your custom post meta is added after the user meta has been imported and updated by WP All Import.

    Here’s an example of how you could use the ‘pmxi_saved_post’ action:

    **add_action(‘pmxi_saved_post’, ‘my_function_after_import’, 10, 1);**
    **function my_function_after_import($post_id) {**
    **// Get the user associated with the imported post**
    **$post_author_id = get_post_field(‘post_author’, $post_id);**

    **// Get the user meta**
    **$stand_number = get_user_meta($post_author_id, ‘stand_number’, true);**
    **if (!empty($stand_number)) {**
    **add_post_meta($post_id, ‘stand_number’, $stand_number);**
    **} else {**
    **// Add some error handling or logging in case the user meta is still empty**
    **}**
    **}**

  2. Is WP All Import using the builtin `wp_insert_user()` function to insert users, or are they using their own function? If they are using their own function they may be skipping the `user_register` hook.

  3. Did you check wpallimports hooks? They have tons and you should be able to run your function on whatever hook fires after it completes

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer