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

Find post id of CPT WordPress

  • SOLVED

Hi,

I need help to display ACF fields on a page, not a post. I have a CPT called 'agency_accounts'. A logged in user submits a form that posts the data to an ACF Field Group called 'Agency Details'. In CPT post I can display all the data collected from the form, but I would like to display some of the data on a page on the frontend.

If I use $value = get_field( "agency_business_name", 3166 );
echo $value;
I get the correct field data to display because the post id is static.

What I need though, is to find the post id of the post the current logged-in user submitted so that I can use
$value = get_field( "agency_business_name", $post->ID );
echo $value;


Any help to achieve this would be appreciated

Answers (2)

2019-10-15

Alex Miller answers:

If the user is submitting the post / form then you could make them the author of that post. If they're not already the author you can do this during the submission process.

There's 2 ways you could do this: Shortcode or Function.

Both the below methods could go into a plugin file or into the functions.php file.

Shortcode
The benefit of a shortcode is that it will allow your users to put them in them into the page editor. The shortcode could look something like this:

/**
* Display agency data by user ID
*
* Grab the current logged in user data
* [display_agency_data]
*
* Grab a specific user data
* [display_agency_data user="1"]
*
* @param Array $args
*
* @return String $html
*/
function prefix_display_agency_data_callback( $args = array() ) {

$html = '';
$user_id = get_current_user_id();

// Grab user ID if it's been passed to our shortcode
if( isset( $args['user'] ) ) {
$user_id = intval( $args['user_id'] );
}

// We do not have a user to work with.
if( empty( $user_id ) ) {
return $html;
}

/**
* Grab the single latest post of the post type where the current user is the author
*
* By grabbing the entire post object and not just the single ID, WordPress adds
* all the postmeta into the object cache.
*
* The `no_found_rows` parameter since we do not paginate.
*/
$accounts = get_posts( array(
'post_type' => 'agency_accounts',
'post_status' => 'publish',
'author' => $user_id,
'posts_per_page'=> 1,
'no_found_rows' => true,
) );

// We do not have any accounts to work with
if( empty( $accounts ) ) {
return $html;
}

// Grab the account post ID
$account_id = $accounts[0]->ID;

// Grab the business name by account post ID
$html = get_field( 'agency_business_name', $account_id );

/**
* Shortcodes return HTML instead of echoing values.
* Take advantage of PHP `ob_start()` and `ob_get_clean()` functions if necessary
*/
return $html;

}
add_shortcode( 'display_agency_data', 'prefix_display_agency_data_callback' );


You could add it into the page editor as [display_agency_data] which will grab the logged in user if it can. If you want to look for a specific user you can say [display_agency_data user="1"] where "1" is the user ID you're looking for. Finally, if you want to add this into a PHP template you can use do_shortcode( '[display_agency_data]' ); but if you plan on using it in a PHP template, use the below function example instead. It's very similar to the above.

Function

You could add the below directly into a functions.php file or a separate plugin file:

/**
* Display agency name by user ID
*
* @param Integer $user_id
*
* @return String $name
*/
function prefix_get_agency_name( $user_id = 0 ) {

$name = '';

// Grab user ID if it's been passed to our function
if( empty( $user_id ) ) {
$user_id = get_current_user_id();
}

// We do not have a user to work with.
if( empty( $user_id ) ) {
return $name;
}

/**
* Grab the single latest post of the post type where the current user is the author
*
* By grabbing the entire post object and not just the single ID, WordPress adds
* all the postmeta into the object cache.
*
* The `no_found_rows` parameter since we do not paginate.
*/
$accounts = get_posts( array(
'post_type' => 'agency_accounts',
'post_status' => 'publish',
'author' => $user_id,
'posts_per_page'=> 1,
'no_found_rows' => true,
) );

// We do not have any accounts to work with
if( empty( $accounts ) ) {
return $name;
}

// Grab the account post ID
$account_id = $accounts[0]->ID;

// Grab the business name by account post ID
$name = get_field( 'agency_business_name', $account_id );

/**
* You can modify this function however you see fit.
* This example simply returns a name.
*/
return $name;

}


You can call this directly in your PHP files either by passing a specific user ID or by default it will get the currently logged in user:

<?php
/**
* Get current user agency name
*/
echo prefix_get_agency_name();

/**
* Get agency name by user ID 1
*/
echo prefix_get_agency_name( 1 );
?>


Hopefully the above makes sense and is helpful! Otherwise, with more information I can come back and improve my answer.


mario_kart_tour_hack comments:

hmm.. give me a minute


Tim comments:

Thank you Alex.

This works just perfectly now, both options worked, but I am using the function rather than shortcode. Thank you. Is it possible to add more ACF fields from the same ACF Agency Accounts field group to this function?


Alex Miller comments:

Now that you have the account post ID you can get whatever fields are attached to that post ID.

get_field( 'field_name', $account_id )

This is of course using the function above to get the account ID and such.

2019-10-15

Cesar Contreras answers:

try using the get_current_user_id() function:

$value = get_field( "agency_business_name", get_current_user_id() );
echo $value;


Tim comments:

Thanks Cesar,

but this did not work. For whatever reason the current user id can be echoed correctly on the page but the ACF get field returns null with current user id


Cesar Contreras comments:

can you try using global $current_user

global $current_user;
$value = get_field( "agency_business_name", $current_user->ID );
echo $value;


Cesar Contreras comments:

and validate if the user is logged or not


global $current_user;
$user_id = $current_user->ID
$value = get_field( "agency_business_name", $user_id );

if ($user_id == 0) {
echo 'You are currently not logged in.';
} else {
echo 'You are logged in as user '.$user_id;
echo $value;
}


obviously print a message or what to do if you are not logged in is up to you


Tim comments:

No this did not work, thanks for your effort though.