0

I'm trying to get latest post ID.

And then store that ID into user_meta.

Right now I have this code:

add_action('wp', 'bt_update_user_homepage_meta');
function bt_update_user_homepage_meta () {
    
// Get user id
$user_id = get_current_user_id();

// Get newest post ID
        //WP_Query Arguments
$args = array(
    'posts_per_page'    => 1,
    'meta_key'          => 'usp-custom-1',
    'meta_value'        => 'question',
);

//Execute WP_Query (Results placed in $the_query)
$the_query = new WP_Query($args);
$id = get_the_ID();
     
//The WordPress Loop
if ($the_query->have_posts()) { //Check if there are any posts returned in $the_query

    while ($the_query->have_posts()) { //Loop through posts returned in $the_query
        $the_query->the_post();

//Update user meta with the latest post ID
         if ( is_page( array( 'home' ) ) ) update_user_meta($user_id, 'pagehome', $id);
    }

} 

//Reset WordPress Post Data
wp_reset_postdata();

But it stores current post ID, instead of the latest post ID.

Been struggling with this for a while.

I know I maybe missing something in the code.

Need help.

3
  • 1
    $id = get_the_ID(); is outside the loop. It needs to be after $the_query->the_post() to get the ID of the post inside the query. Commented Jul 14, 2021 at 14:05
  • You should also elaborate on what you're ultimately trying to achieve. Writing to the database on every request is not a good idea. Commented Jul 14, 2021 at 14:06
  • Brilliant! it worked simply by putting $id = get_the_ID(); inside the query! Thanks Jacob. I thought I was missing something very small. I have also used if ( !empty( $user_id ) && is_page( [ 'home' ] ) ) to process only logged in users. THANKS AGAIN!
    – robert0
    Commented Jul 14, 2021 at 14:22

1 Answer 1

0

Here is the code that should probably work for you:

function bt_update_user_homepage_meta() {

    // Get user id
    $user_id = get_current_user_id();

    if ( !empty( $user_id ) && is_page( [ 'home' ] ) ) {

        $posts = wp_get_recent_posts( array(
            'numberposts' => 1,
            'meta_key'    => 'usp-custom-1',
            'meta_value'  => 'question'
        ) );

        if ( !empty( $posts ) ) {
            update_user_meta( $user_id, 'pagehome', array_pop( $posts )->ID );
        }
    }
}

add_action( 'wp', 'bt_update_user_homepage_meta' );

I've tweaked things:

  • I've wrapped it up in the if ( !empty( $user_id ) && is_page( [ 'home' ] ) ) so that you don't do any of this processing unless the user is logged-in and it's the page "home"
  • I use the handy wp lookup function wp_get_recent_posts for recent posts that orders them by the newest published, and limit the query to 1 result.
  • then I pop it off the array of posts and reference the ID to store it into the user meta.

For completeness sake, I should say that you probably shouldn't be doing this on every time the page is loaded as you'll have that extra get_posts() query each time, albeit for only logged-in users. There isn't really an issue with updating the user meta though, since if the value isn't changed, no data is written to the DB.

2
  • Hey Paul G, thanks for your effort, I have actually fixed my code simply putting the $id = get_the_ID(); inside the query and used your if ( !empty( $user_id ) && is_page( [ 'home' ] ) ) so that only logged in users on the page "home" would be proccessed. THANKS AGAIN for your time!
    – robert0
    Commented Jul 14, 2021 at 14:24
  • no problem. If my code helped you, please do feel free to vote it or accept. Thanks.
    – Paul G.
    Commented Jul 14, 2021 at 14:41

Your Answer

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

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