• Resolved anurag.deshmukh

    (@anuragdeshmukh)


    I have one registration form and once user is registered, I have created an user and created new post in Custom Post type where I have passed/assigned newly created User ID to that post. This works fine.

    Now I want users to edit their post from frontend and I have added few custom fields to save various data. So now, in order to edit their data specially for custom fields, I need that post ID.

    I used get_posts and even WP_Query to get post added by particular author ID, I get data but I want post ID of that post that they are editing. Right now I am not getting that and due to that I am not able to update my custom fields.

    Author ID is 40 and post ID is 355 (I checked from admin panel)

    
    $args = array(
    	'author'        =>  $author_id,
    	'orderby'       =>  'post_date',
    	'order'         =>  'ASC',
    	'post_type'	=> 'search_doctors',
    	'posts_per_page' => 1
    );
    
    get_posts($args);
    

    Author ID is correct. Need help on getting post ID. Thanks in advance.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Something like this?

    $posts = get_posts( $args );
    $post = $posts [0];
    
    echo $post->ID;
    Thread Starter anurag.deshmukh

    (@anuragdeshmukh)

    Yes, I tried that but its getting / displaying same author ID what we inputted in $args.

    Moderator bcworkz

    (@bcworkz)

    You’re saying $author_id == $post->ID? While it is in theory possible, very unlikely, yet still correct. Are you sure you’ve identified your IDs correctly? Either that or some other code is confusing the issue. The code works as expected on my site, once I changed the post type to one of my CPTs and adding $author_id = 1;. I knew it would, I felt silly even testing it, but I’ve missed things before. Not this time.

    I highlydoubt that you’re getting a post ID that’s equal to the authro ID. As bcworkz sad, it’s possible, but very highlty unlikely.

    You need to do some very basic de-bugging to see what you are sending, and what’s being returned. Try this:

    echo "<p>Author ID: '".$author_id."'</p>";
    
    $args = array(
    	'author'        =>  $author_id,
    	'orderby'       =>  'post_date',
    	'order'         =>  'ASC',
    	'post_type'	=> 'search_doctors',
    	'posts_per_page' => 1
    );
    
    $posts = get_posts($args);
    
    echo "<pre>";
    var_export ($posts);
    echo "</pre>";

    You should end up with an array containing one WP_Post object. From there you’ll be able to see what you aren sending, and what’s being returned, and that should point you towards what the problem is.

    Thread Starter anurag.deshmukh

    (@anuragdeshmukh)

    Thank you guys for the support.

    @bcworkz $author_id == $post->ID both my ID’s are different say author ID was 40 and post ID was 311.

    @catacaustic yes I got WP_Post object and in that I got the desired ID. get_posts and WP_Query returns object in which we get ID’s but I was not getting on one post.

    May be any junk data or something, I was not getting ID for one particular post. I deleted that post and user and once again checked and all was working fine. That’s a bit strange but now it’s working fine.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Get Post ID when queried using author ID’ is closed to new replies.