Support » Themes and Templates » Get pages by template only works with some templates that have no parent

  • apax27

    (@apax27)


    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?

Viewing 4 replies - 1 through 4 (of 4 total)
  • apax27

    (@apax27)

    anyone?

    apax27

    (@apax27)

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

    SpaceDogDeveloper

    (@spacedogdeveloper)

    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.

    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 />';

    🙂

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Get pages by template only works with some templates that have no parent’ is closed to new replies.