WordPress.org

Ready to get started?Download WordPress

Forums

Get pages by template only works with some templates that have no parent (5 posts)

  1. apax27
    Member
    Posted 2 years ago #

    I have spent hours upon hours searching for this answer and I am about to rip my hair out!! It may be some kind of weird bug but I just want to make sure there's nothing I'm doing wrong

    So I have this

    $pages = get_pages(array(
    	'meta_key' => '_wp_page_template',
    	'meta_value' => 'product.php'
    ));
    foreach($pages as $page){
    	echo $page->post_title.'<br />';

    Which should return a list of page titles for my template product.php. But it doesn't. So I played around and tried different templates- some worked and some didn't.

    Then I realized, all pages that have a parent page will not show up. When I changed one of the pages with the product.php template to "no parent", that page displayed in my results.

    I need these pages to have parent pages, so how can I display them by template?

  2. apax27
    Member
    Posted 2 years ago #

    anyone?

  3. apax27
    Member
    Posted 2 years ago #

    Bumping this post. I am desperate for an answer. I have been trying to figure this out for months and searched everywhere!!

  4. SpaceDogDeveloper
    Member
    Posted 2 years ago #

    Long shot here, but have you tried doing a new DB query using wp_query before trying to get the pages?

    http://codex.wordpress.org/Class_Reference/WP_Query

    It could be that you're trying to find pages outside of the current loop you're running.

  5. alesub
    Member
    Posted 1 year ago #

    Hi,

    It's because you should use get_posts() instead, like this:

    $pages = get_posts(array(
            'post_type' => 'page',
    	'meta_key' => '_wp_page_template',
    	'meta_value' => 'product.php'
    ));
    foreach($pages as $page){
    	echo $page->post_title.'<br />';

    :)

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.