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

Conditional IF Custom Post Type is Root, A Parent, A Child WordPress

  • SOLVED

Hi,

I need a conditional statement to understand if a custom post type single is a parent, child or standalone.

My code works fine for pages … but can't seem to get it to work with custom post types…


<?php
global $post;
$children = get_pages( array( 'child_of' => $post->ID ) );
$parent_ID = $post->post_parent;

/* IF POST IS A CHILD */ if ( is_page() && $post->post_parent ) : ?>

THIS IS A CHILD!

<?php /* IF POST IS A PARENT */ elseif ( is_page() && count( $children ) > 0 ) : $post_ID = get_the_ID(); ?>

THIS IS A PARENT!

<?php /* IF POST IS ROOT */ else : ?>

THIS IS NOT A PARENT OR A CHILD!

<?php endif; ?>


<strong>Ideas:</strong>

I am not sure if I need to add 'capability_type' => 'page', to my post type
Use ‘get_posts’ instead of ‘get_pages’

<em>Any suggestions you can provide would be much appreciated!</em>

UPDATE:

This almost works!


<?php
global $post;
$children = get_posts( array( 'child_of' => $post->ID ) );
$parent_ID = $post->post_parent;
?>
<?php /* IF POST IS A CHILD */ if ( is_single() && $post->post_parent ) : ?>

THIS IS A CHILD!

<?php /* IF POST IS A PARENT */ elseif ( is_single() && count( $children ) > 0 ) : $post_ID = get_the_ID(); ?>

THIS IS A PARENT!

<?php /* IF POST IS ROOT */ else : ?>

THIS ISNT A CHILD OR PARENT

<?php endif; ?>


Except orphans show as parents ...

Answers (3)

2014-01-31

John Cotton answers:

How about this?

It does what you need directly in SQL, which may not be completely desirable if you like sticking with the API.

But it's more efficient AND will work with any post type.


define( 'CHILD_POST', 1 );
define( 'PARENT_POST', 2 );
define( 'ORPHAN_POST', 3 );

function get_post_family_tree( $post_id, $post_type = 'page' ) {
global $wpdb;

$r = $wpdb->get_row( "SELECT post_parent, child_count FROM $wpdb->posts p, (SELECT COUNT(id) as child_count FROM $wpdb->posts WHERE post_parent = $post_id AND post_status = 'publish' AND post_type = '$post_type') AS c WHERE p.id = $post_id AND p.post_status = 'publish' AND p.post_type = '$post_type'" );

if( $r->post_parent > 0 ) {
return CHILD_POST;

} else if( $r->child_count > 0 ) {
return PARENT_POST;

} else {
return ORPHAN_POST;
}
}


John Cotton comments:

How about this?

It does what you need directly in SQL, which may not be completely desirable if you like sticking with the API.

But it's more efficient AND will work with any post type.


define( 'CHILD_POST', 1 );
define( 'PARENT_POST', 2 );
define( 'ORPHAN_POST', 3 );

function get_post_family_tree( $post_id, $post_type = 'page' ) {
global $wpdb;

$r = $wpdb->get_row( "SELECT post_parent, child_count FROM $wpdb->posts p, (SELECT COUNT(id) as child_count FROM $wpdb->posts WHERE post_parent = $post_id AND post_status = 'publish' AND post_type = '$post_type') AS c WHERE p.id = $post_id AND p.post_status = 'publish' AND p.post_type = '$post_type'" );

if( $r->post_parent > 0 ) {
return CHILD_POST;

} else if( $r->child_count > 0 ) {
return PARENT_POST;

} else {
return ORPHAN_POST;
}
}


West Coast Design Co. comments:

Hey John,

I appreciate the outside the box approach; I gave it a try (defined my own post type) echo’ed some messages … not getting anything in return.

Hmmmm


John Cotton comments:

Any error message? It must return something unless it fails.

I assume you calling it like this:

$post_id = 17; $r = get_post_family_tree( $post_id, 'my_post_type' );

?


John Cotton comments:

After the $wpdb->get_row add a line:

echo "{$wpdb->last_query} :: {$wpdb->last_error}";

just to see if there is an error.

If not, try run the query that's output in PHPMyADmin and see what happens.


West Coast Design Co. comments:

Yea, didnt get anything. I am back on track with my original question ... well lol except for the API conflicting :)


John Cotton comments:

<blockquote>Yea, didnt get anything.</blockquote>

You can't be using it correctly.

Either you get an error, or you get output. One of those things must be true.

Perhaps if you posted your code it would help me see where it's going wrong.

2014-01-31

Arnav Joy answers:

what problem are you getting now?


West Coast Design Co. comments:

Hi Arnav,

This now works for Parents & Childs however is preforming oddly with ‘orphans’.

On an ‘orphan’ single post type:

$children = get_children( array( 'child_of' => $post->ID) );

Should var_dump: ‘array(0) { }' instead its showing an array of ALL the first single post types (not children)

… this whole thing works flawlessly with pages :)

2014-01-31

Bob answers:


<?php

global $post;
$children = get_pages( array( 'child_of' => $post->ID, 'post_type' => 'your-custom-post-type' ) );
$parent_ID = $post->post_parent;

/* IF POST IS A CHILD */ if ( is_page() && $post->post_parent ) : ?>
THIS IS A CHILD!

<?php /* IF POST IS A PARENT */ elseif ( is_page() && count( $children ) > 0 ) : $post_ID = get_the_ID(); ?>

THIS IS A PARENT!

<?php /* IF POST IS ROOT */ else : ?>

THIS IS NOT A PARENT OR A CHILD!

<?php endif; ?>


Bob comments:

try adding your custom post type parameter in get_pages function see what happens.


<?php

global $post;
$children = get_pages( array( 'child_of' => $post->ID, 'post_type' => 'your-custom-post-type' ) );
$parent_ID = $post->post_parent;

/* IF POST IS A CHILD */ if ( is_page() && $post->post_parent ) : ?>
THIS IS A CHILD!

<?php /* IF POST IS A PARENT */ elseif ( is_page() && count( $children ) > 0 ) : $post_ID = get_the_ID(); ?>

THIS IS A PARENT!

<?php /* IF POST IS ROOT */ else : ?>

THIS IS NOT A PARENT OR A CHILD!

<?php endif; ?>


West Coast Design Co. comments:

This just shows the else. :(


Bob comments:

it is because of is_page() in your condition

here is new code.
Notice is_single() function



<?php
global $post;

$children = get_pages( array( 'child_of' => $post->ID, 'post_type' => 'your-custom-post-type' ) );

$parent_ID = $post->post_parent;

/* IF POST IS A CHILD */ if ( is_single() && $post->post_parent ) : ?>

THIS IS A CHILD!
<?php /* IF POST IS A PARENT */ elseif ( is_single() && count( $children ) > 0 ) : $post_ID = get_the_ID(); ?>
THIS IS A PARENT!
<?php /* IF POST IS ROOT */ else : ?>
THIS IS NOT A PARENT OR A CHILD!

<?php endif; ?>


West Coast Design Co. comments:

Sorry, soo tired. IT WORKS!!!! IT REALLY WORKS LOVE YOU SOOO MUCH RIGHT NOW!!

Thank everyone!!!


Bob comments:

I am glad to know that it works! :)
Please mark this question as completed and vote. :)