Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Retrieve Post ID by Author (current user) and CPT WordPress

  • SOLVED

I am looking for a function to return a post ID based on the current user (who would be the author of the post) and a set custom post type. As well as then spit out an array() of the meta for that post.

The custom post type is the profile page of the current user that is created after registration. The function is being used to retrieve and populate a form with the profile information so it can be editted. Each user will have only one post with the custom post type

Specifics:
Post type: "guide_profile"
Post type template: "single-guide_profile.php"
Sample meta: "wpcf-experience"

Answers (2)

2012-09-26

John Cotton answers:


global $current_user;

// Get the post(s) for the current user.
$posts = new WP_Query( array( 'author' => $current_user->ID ) );
$custom_type = $posts->posts[0]->post_type; // Is this what you mean?

// Or this..?
//$posts = new WP_Query( array( 'author' => $current_user->ID, 'post_type' => 'guide_profile' ) );



// Get the meta data for that post
$meta = get_post_custom( $posts->posts[0]->ID );

// Do what you need to...


Kyle comments:

Thanks for the reply! This worked


Kyle comments:

BTW it was the second function that I ended up using

2012-09-26

Arnav Joy answers:

check this

<?php

if ( is_user_logged_in() ) {
global $current_user,$wpdb;
$row = $wpdb->get_col("SELECT ID FROM ".$wpdb->prefix." WHERE post_author=".$current_user->ID." AND post_type='guide_profile' ");
$post_id = $row->ID;

$wpcf-experience = get_post_meta($post_id,'wpcf-experience',true);

}
?>


$post_id has the id of the post you want and $wpcf-experience has the meta data