0

I would like to create a custom query where the post ID is equal to a value which is stored in a variable. If I do a var_dump of the variable I can see the ID that I want to use.

However I don't know how I can target that specific ID and use it in my query. I'm really new to this more advanced PHP work and I'm probably searching with the wrong search terms to get the answer.

This is a part of the var_dump of the $account where the ID is 164.

object(WP_Post)#4451 (24) { ["ID"]=> int(164) 

This ID should be used within the query to target the specific post ID like:

$args = array(
'post_type' => 'account',
'p' => $account->ID,
 );

Can this be done? And if so, how can alter the query to do this?

Edit: Updated the query above with the correct code. Also updated the right variable name in the original question.

Thanks in advance.

  • So to make sure, you want to query a unique custom post having the ID 164? – ClemC Aug 18 '17 at 11:59
  • Yes, just a single and unique post where the ID is equal to the ID stored in the variable. In this case 164 – NielsPilon Aug 18 '17 at 12:00
  • "$account_id" is a very strange name for a var containing a post object... I doubt you mean what you posted in your question... Furthermore, "$account_id" seems to be or to contain the post you want to query... So do you really need this query? – ClemC Aug 18 '17 at 12:19
  • Guess that I wasn't clear as I edited the original question. I made a typo with the variable name, it should be $account. I need the query afaik because the ID stored in the variable is a value of a custom user meta field. – NielsPilon Aug 18 '17 at 12:23
  • It's clearer now, but still, if I refer to your var_dump( $account ) output, it's containing a WP_Post object with a property $ID = 164... So it can only be the post you are already looking for... In this case, if you have already the post, you don't need to query it...^^" – ClemC Aug 18 '17 at 12:32
0

Very simply use get_post()...

$my_post = get_post( $account->ID );
  • Probably me question wasn't clear but you pointed me in the right direction. I got it solved with 'p' -> $account->ID,. This returns the correct post ID of the CPT that I wanted to query. – NielsPilon Aug 18 '17 at 12:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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