Skip Navigation

[Résolu] Find if my post has a parent, and what that parent is (in PHP)?

This support ticket is created Il y a 7 années et 9 mois. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 5 réponses, has 3 voix.

Last updated by kelvinL-2 Il y a 7 années et 8 mois.

Assigned support staff: Shane.

Auteur
Publications
#288335

I am writing a custom dashboard plugin for my site. I need to find out if a specific post has a parent assigned to it (belong to a parent) and if so, what the ID of that parent is. Because I am writing PHP, I need to do this in PHP. I can only find how to do this in CRED, not PHP. Please help! Thanks.

#288466

Shane
Supporter

Languages: Anglais (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Steve,

Thanks for contacting us.

This is certainly possible as wordpress has some functions that you can use to check this.

One way of doing it is by getting the particular post by using this function.

<?php get_post( $post_id ); ?> 

This will return a post from the id specified in the function. Once you have done that you need to store the post object in a variable like this.

<?php 

$val = get_post( $post_id );

 ?> 

Once you have done that you need to check if the post_parent has a value in it.
Here is an example of how to do that.

if($val->post_parent != 0){
do something if there is a parent
}else{
do something if there is no parent
}

So we evaluate that the post_parent value is not equal to 0 since 0 is the default value.
When the value is 0 then the post does not have a parent.

We evaluate to determine if there is a parent.

Once there is a parent then you can manipulate the code to get a value for when there is a parent.

Likewise when there is not.

Please take a look at this document here for a breakdown of the wordpress Post Object http://codex.wordpress.org/Class_Reference/WP_Post

Please let me know if this was helpful.

#291037
Screen Shot.jpg

Hi Shane,

I apologize for my late response. I have been so busy I have not had time to write code.

Unfortunately your solution does not seem to be working for me.

I have maybe explained myself wrong. All Posts are assigned ("belongs to") a Company (a company is a custom post type). See the attached screenshot I added to this forum reply.

Despite assigning a post, $val->post_parent is not set.

Kind regards,

Steve

#291324

Shane
Supporter

Languages: Anglais (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Steve,

Thanks for the clarity.

In this case you will need to use this function here.

<?php wp_get_post_parent_id( $post_ID ); ?> 

This function will the Post parent ID if there is any.
If there is no parent then the function will return False.

Please see here for reference : http://codex.wordpress.org/Function_Reference/wp_get_post_parent_id

Let me know if this helps

#291443

That did not work but after spending a long time I found I must use the _wpcf_belongs_* key in the database to lookup if there is a parent.

Thanks.

#294048

Dear steveJ-3,

Do you mind sharing your code how you can lookup the database and check if there is a parent post?

Kelvin