I have created a template named product.php
with the following header:
<?php
/*
Template Name: Product Page
*/
How can I list, in a sidebar, every page that uses the “Product Page” template?
I have tried reading the wp_list_pages()
function documentation, but it only seemed possible to list filtering by post_type
, not by template used.
You can do this with a
WP_Query
meta_query
. The page template filename is stored in post meta under the key_wp_page_template
:the page template is setted via a meta field with the key
'_wp_page_template'
. The value for this meta field is the php full file name of the template file, something like‘
page-products.php'
so you can create a custom function to easyly get the page with a specific template using meta_key and meta_value param off
get_pages
(or using aWP_Query
with'meta_query'
argument):This function accepts as first (mandatory) argument the template and as second (optional) argument all the arguments of get_pages.
the template can passed with or without ‘.php’ extension:
or
After that, you can use the page retrieved as you want: loop throu them and output some markup, create a custom widget that make use of the function, and so on…