WordPress.org

Make WordPress Core

Opened 3 years ago

Last modified 3 years ago

#16264 new feature request

Add function get_pages_by_template()

Reported by: mikeschinkel Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 3.1
Component: Posts, Post Types Keywords: has-patch
Focuses: Cc:

Description

This patch adds a function get_pages_by_template() to /wp-includes/post.php. It accepts a page template string and returns an array of post objects keyed by the pages that use the template:

$pages = get_pages_by_template( 'page-about.php' );

Using WordPress for CMS I've been finding a need to provide links to specific pages in a more robust way than hardcoding their post_id or even referencing their post_name/URL slug and the way I've identified that works well is to locate a page by it's page template. Here a function that a theme might write that would use the function I'm proposing be added:

function get_about_page_link() {
  $pages = get_pages_by_template( 'page-about.php' );
  if ( count( $pages ) )
    return get_page_link( get_post( $pages[0] )->ID);
  return false;
}  

Unfortunately WordPress does not have efficient functionality in core that would allow a themer to lookup a page or a list of pages by their templates thus requiring the theme developer to resort to SQL which is ideally avoided.

Thus I'm providing this patch to enable a themer to do this lookup without having to resort to SQL.
The patch also fixes a typo that affects phpdoc in the same file: @parem => @param.

Attachments (2)

get_pages_by_template.diff (1.3 KB) - added by mikeschinkel 3 years ago.
Adds get_pages_by_template() and fixes a phpdoc comment typo
get_pages_by_template-v2.diff (1.2 KB) - added by mikeschinkel 3 years ago.
Updated get_pages_by_template()

Download all attachments as: .zip

Change History (8)

mikeschinkel3 years ago

Adds get_pages_by_template() and fixes a phpdoc comment typo

comment:1 mikeschinkel3 years ago

  • Keywords has-patch added

comment:2 mikeschinkel3 years ago

Just as soon as I post this I realized that there is a way to get this without SQL. I'll upload another patch which will limit the justification for why this should be added, taking it from a must-have to a nice-to-have.

mikeschinkel3 years ago

Updated get_pages_by_template()

comment:3 mikeschinkel3 years ago

I've just uploaded another version that uses WP_Query instead. It loosed the ability to lookup with a SQL LIKE and to return the values keyed by post ID but it also uses built-in functionality and is probably faster.

Version 0, edited 3 years ago by mikeschinkel (next)

comment:4 F J Kaiser3 years ago

I guess it would make more sense to set the default orderby value to menu_order as this will IMHO be used for menus mostly(?) and therefore should order by the default setting. Ordering by date sounds like an archive job.

comment:5 follow-up: nacin3 years ago

menu_order doesn't have an index.

comment:6 in reply to: ↑ 5 F J Kaiser3 years ago

Replying to nacin:

menu_order doesn't have an index.

I know. But post_date doesn't make sense for pages. Further: You (hopefully) won't have thousands of pages.

Note: See TracTickets for help on using tickets.