WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I created a hierarchical custom post type called "films." It is hierarchical so it can have children elements.

I am using a conditional statement in my single-films.php to show different content for the parent page from the children pages like so:

<?php if( $post->post_parent != 0 ) {
    echo "Press Page";
} else {
    echo "Film Page";
} ?>

In the condition that the page is a child, I want to use a custom wordpress loop, that loops through it's parents content. I am guessing I will need to use a loop similar to this (for the children):

<?php $posts = query_posts('&post_parent=0'); ?>
<?php while(have_posts()) : the_post(); ?>

wordpress content

<?php endwhile; ?>

This isn't working correctly. I need it to pull any content I wish (custom field, title, featured image, etc) from it's parent. Any idea how I can modify this loop to pull content from it's parent?

share|improve this question

marked as duplicate by toscho Feb 15 '13 at 20:27

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

Are you trying to loop through ONLY the parent, or are you trying to loop through the parent and the child? – mor7ifer Mar 18 '12 at 17:32
only the parent – JCHASE11 Mar 18 '12 at 17:52
Avoid query_posts, it should never be used, it will only cause you pain, headaches, nausea, and horrible migraine induced vomitting – Tom J Nowell Sep 30 '12 at 3:07

Use the following to change the query from querying the current page to querying the parent of the current page:

query_posts( 'p='.$post->post_parent  );

That will change the $wp_query object to hold only the parent of the current post.

Note: this is untested.


You can also do it with a custom WP_Query as so:

$args = array(
    'p' => $post->post_parent
$parent = new WP_Query( $args );

while( $parent->have_posts() ) : $parent->the_post();
    // loop stuff here
share|improve this answer
yea, this solution isnt working for me. THe logic makes sense... – JCHASE11 Mar 18 '12 at 18:14
How does it fail? – mor7ifer Mar 18 '12 at 18:37
it has some strange permalink behavior and spits out an error: Fatal error: Cannot redeclare curpageurl() (previously declared – JCHASE11 Mar 18 '12 at 18:51
Try the updated method that I just posted, looks like query_posts() wants to be difficult. – mor7ifer Mar 18 '12 at 19:04
I tried it with no luck. Thanks for your efforts though, it seems that this isn't a common problem. What is the 'p'? – JCHASE11 Mar 18 '12 at 19:26

Use get post meta to extract the parents custom field:

for an image:


$image = wp_get_attachment_image_src(get_post_meta($post->post_parent, 'custom-field-key', true));
echo '<img src="' . $image[0] . '" />';

For a text custom field:

<?php $variable = get_post_meta($post->post_parent, 'custom_field_name', true); ?>
share|improve this answer

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