[resolved] wp_query and post_parent -> not working (6 posts)

  1. petra5
    Posted 3 years ago #

    On my front page of the site (which uses page.php template) I want to display a list of child pages from parent with id 19, I want to display a featured image, title of a page and a specific custom field of each page.
    For this purpose I tried to use wp_query but it does not seem to work. I looked into WordPress codex where one of the parameters to use with wp_query is:
    post_parent (int) - use page id. Return just the child Pages.
    I don't understand why my code doesn't work. I am using:

    $query = new WP_Query('post_parent=19'); and the loop:
    while ( $query->have_posts() ) : $query->the_post(); ?>
    <?php the_post_thumbnail( 'small' ); ?>
    <?php the_title(); ?>
    <?php echo get_post_meta($post->ID, 'intro', true); ?>
       <?php endwhile;
    		   // Reset Post Data

    I have a parent page with Id 19 set up and relevant subpages. The query above doesn't not bring any data.

    On the same page I also have another loop like this (before above one):

    <?php if ( have_posts() ) : ?>
    				<div id="welcome">
    				<?php while ( have_posts() ) : the_post(); ?>
    		<h1><?php the_title(); </h1>
    <?php the_content(); ?>
    				<?php endwhile; ?>
                           <?php wp_reset_query(); wp_reset_postdata(); ?> </div>
    			<?php endif; ?>

    I wonder if there is a conflict? Or what is that that I am doing wrong? Am I missing something? Your help would be really appreciated.

  2. keesiemeijer
    Posted 3 years ago #

    Try it with this [untested]:

    $query = new WP_Query('post_type=page&post_parent=19');
  3. petra5
    Posted 3 years ago #

    ok, I tried and it works! Thanks so much for this!

  4. keesiemeijer
    Posted 3 years ago #

    No problem. Glad you got it resolved.

  5. Achim Meurer
    Posted 3 years ago #

    I have an additional question:

    Is it possible to set the value for "post_parent" with the titel of the page instead its ID?

    e.g. $query = new WP_Query('post_type=page&post_parent=contact');

    I know, this is not working. My question: is there a possibility?

  6. superdeluxesam
    Posted 3 years ago #

    To clarify this matter for anyone who is also puzzled by this, if you check the docs for WP_Query, you'll notice this line when it talks about 'post_type' queries:

    "post_type (string / array) - use post types. Retrieves posts by Post Types, default value is 'post';"

    (my emphasis)

    Bascially, the reason that petra5's query didn't work is that there's a default argument given to the query along the lines of 'post_type=post'.

    If you want to query for any other post type, in this case pages, you need to explicitly set it, as keeslemeijer did with his suggestion.

Topic Closed

This topic has been closed to new replies.

About this Topic