Query a WordPress page by its title (which has a parent page)

I have created a wordpress query which takes the entire content from 1 particular page using the its title

So far i have the following:

Read More
<?php
query_posts('pagename=features');
if (have_posts()) :
while (have_posts()) : the_post();?>

<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
<?php endwhile; ?>
<?php else : ?>
<h3 class="center">No Section available</h3>
<p class="center"><?php _e("No Section available"); ?></p>
<?php endif;
//Reset Query
wp_reset_query();
?>

The main issue is that there is numerous pages which care called ‘features’ but they all have different parents. Is it possible to do this via parent somehow?

For example, here is my hierarchical page structure:

products > designgo silver > features
products > designgo silver > screenshots
products > designgo silver > benefits

products > designgo gold > features
products > designgo gold > screenshots
products > designgo gold > benefits

products > designgo platinum > features
products > designgo platinum > screenshots
products > designgo platinum > benefits

If i could do something like:

<?php
query_posts('pagename=features&parent=designgosilver');
if (have_posts()) :
while (have_posts()) : the_post();?>

that would be perfect,

can anyone give us a pointer?

Cheers!

Related posts

1 comment

  1. You are almost there—but please, use WP_Query.

    $args = array(
        'pagename' => 'features',
        'post_parent' => PARENT-PAGE-ID-HERE,
    );
    $query = new WP_Query($args);
    if ($query->have_posts()) :
        $query->the_post();
    
        // ...
    
        wp_reset_postdata();
    endif;
    

    // EDIT:
    If you don’t want to or cannot use the parent page’s ID, you can access it by its title, for instance like so:

        'post_parent' => ($parent = get_page_by_title('Designgo Silver')) ? $parent->ID : 0,
    

Comments are closed.