Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am using a form (gravity form) on my site. The form is inserted in a page (called form_page) on the users dashboard when they are logged in

http://example.com/dashboard/form_page

I am using a plugin (see below) to dynamically populate the form with content from the post the user is author to. However I need to pass the POST ID to the URL for it to work.

So the URL would end up looking like this

http://example.com/dashboard/form_page?gform_post_id=476

This works.

However, I am manually appending the ?gform_post_id=476 to the end of the URL to test the concept actually works and it does. However I have no idea how to dynamically pass this post ID to the end of the URL of the page for an Author who is logged in.

It might help to know that the "Author" will only have 1 post associated with them. So (out loud) I am thinking there might be a way to GET the POST_ID associated with the logged in USER and then pass it to the URL of that particular page.

I am just not sure how to connect the missing piece of this puzzle and would appreciate some guidance.

Cheers in advance...

My effort so far is as follows

<?php foreach(get_posts(array('author' => $author_id)) as $post)?> <a href="http://example.com/dashboard/form_page?gform_post_id=<?php echo $post->ID ?>">Link</a>

But this seems to get first POST ID the site. Not the ID of the Authors POST. Note: It is a custom post.

Plugin used: http://wordpress.org/extend/plugins/gravity-forms-update-post/

share|improve this question

1 Answer 1

up vote 1 down vote accepted

First of all. The question is a little misleading. What you actually want is "post id(s) for current user".

Here we go:

// Global variable for current user ID
// More information: http://codex.wordpress.org/Function_Reference/get_currentuserinfo
$user_ID;

// You need to create a new WP query object
// More info: http://codex.wordpress.org/Class_Reference/WP_Query
$my_query = new WP_Query( array(
    'post_type' => 'farmers',
    'author' => $user_ID
));

// You get all the current user posts from the query object
$posts = $my_query->posts;

// You get the first post from the posts array
$first_post = $posts[0];

// You get the post ID from post object and store it to $gform_post_id variable
$gform_post_id = $first_post->ID;

Now you can echo that $gform_post_id in your URL

echo 'http://example.com/dashboard/form_page?gform_post_id='.$gform_post_id;

Cheers

share|improve this answer
    
Hi Carl, thanks for the input and great answer...However I cannot get it to work. For some reason I am getting a POST ID returned which this Author is not even an Author of. I know because I just added a test author and manually assigned them 1 post. Does the fact it is a CUSTOM POST TYPE make any difference to your code? Post Type name is Farmers? thanks again, nearly there... –  Redwall Sep 8 '12 at 16:45
    
@Redwall, I edited the code according to your post type comment. When getting the new WP_Query object there is a setting for post type. I changed that to 'farmers'. –  Carl Rannaberg Sep 8 '12 at 16:52
    
Hi Carl, thanks I just figured that out after posting my message :) One thing though that is not really working still. The result seems to always get the first post in the custom posts rather than the first post for that Author...So regardless of the Author logged in, I always get returned the same POST ID in my case 476...Any ideas? –  Redwall Sep 8 '12 at 17:03
    
Sorry I thought u only changed the custom post type name from your first code. When I compared now I see u also changed the line 'author' => $user_ID thanks for your help, excellent work / advice! –  Redwall Sep 8 '12 at 17:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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