Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have this piece of code that works great: it retrieves a particular page from my WordPress database so I can wrap custom code around it in a template. In the instance below it grabs the page called Showreel:

        <?php
        $page = get_page_by_title('Showreel');
        ?>          
        <?php
             $my_id = $page;
             $post_id = get_post($my_id, ARRAY_A);
             $title = $post_id['post_title'];
             $content = $post_id['post_content'];
        ?>
        <h3><?php echo $title;?></h3>                       
        <?php 
            echo $content
        ?>

However, I have conundrum - I have some pages that do not have unique names. How can I retrieve a page only if it is a direct descendant of a certain parent e.g. I would like to retrieve a page under Showreel called The Ruth Rendell Mysteries. My initial thought was that the code below would work, but it doesn't.

        <?php
        $page = get_page_by_title('Showreel->The Ruth Rendell Mysteries');
        ?>          
share|improve this question
    
did you have a look at: codex.wordpress.org/Function_Reference/get_page_by_path –  Michael Jun 21 '11 at 18:52
    
That's excellent. Thanks very much Michael, the following worked... <?php $page = get_page_by_path('Showreels/The Ruth Rendell Mysteries'); ?> <?php $my_id = $page; $post_id = get_post($my_id, ARRAY_A); $title = $post_id['post_title']; $content = $post_id['post_content']; ?> <?php echo $content ?> –  SparrwHawk Jun 21 '11 at 19:35
    
Whoops, it won't let me format the code correctly. Michael, if you post your suggestion as an answer I'll tick it so you get kudos - thanks v much. –  SparrwHawk Jun 21 '11 at 19:36
    
i've posted the answer - thanks ;-) –  Michael Jun 21 '11 at 20:41
add comment

migrated from stackoverflow.com Jun 21 '11 at 18:05

This question came from our site for professional and enthusiast programmers.

2 Answers

up vote 0 down vote accepted

have a look at:

get_page_by_path()

codex.wordpress.org/Function_Reference/get_page_by_path

share|improve this answer
add comment

As you already discovered, the get_page_by_title function does not accept arguments in that fashion. However, if you look through the available functions at the Wordpress Docs site, you will find get_page_children():

http://codex.wordpress.org/Function_Reference/get_page_children

In the code examples, they retrieve the page object with the get_page_by_title function and then get all of the children of that page. From there, you could separate out the page you wanted.

To see other functions and how they are used take a few minutes to browse through the rest of the Function Reference for some good examples.

Hope this helps and good luck!

share|improve this answer
    
Hi, That article seems useful and I've tried to incorporate it it into my code. I've successfully made it spit out meta-data about the posts, but can't get any further than this unfortunately. I don't have much experience with WordPress coding. –  SparrwHawk Jun 21 '11 at 18:43
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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