WordPress.org

Make WordPress Core

#24763 closed enhancement (fixed)

Add filter to query in get_page_by_path function

Reported by: zbtirrell Owned by: johnbillion
Milestone: 3.9 Priority: normal
Severity: normal Version: 3.6
Component: Posts, Post Types Keywords: has-patch
Focuses: Cc:

Description

We would like to be able to filter the query that is executed in get_page_by_path() so that we can have pages be children of a custom post_type.

To do this currently, we have to add a filter to 'query' and look for this specific query. This approach is unfortunately, quite expensive.

I am attaching a single line diff file that applies a filter to this query called 'get_page_by_path_request'.

Attachments (4)

get_page_by_path_request.patch (821 bytes) - added by zbtirrell 22 months ago.
patch file for the suggested change
get_page_by_path.patch (1.5 KB) - added by zbtirrell 15 months ago.
make the get_page_by_path function support an array of post types
get_page_by_path_v3.patch (1.5 KB) - added by zbtirrell 14 months ago.
a 3rd version of this patch, only adds attachment when post type is scalar
get_page_by_path_v4.patch (2.7 KB) - added by zbtirrell 14 months ago.
this patch also adds post type array support to get_page_by_title

Download all attachments as: .zip

Change History (14)

@zbtirrell22 months ago

patch file for the suggested change

comment:1 @jeremyfelt15 months ago

  • Keywords reporter-feedback added

Hi @zbtirrell, thanks for the request and patch.

What alternative queries do you visualize using through this filter? I don't think we would want to apply a filter directly to the query itself. It is worth discussing what else could be added to help achieve your goal.

comment:2 follow-up: @zbtirrell15 months ago

We are specifically interested in adding additional post types, so this is the part that we are most interested in having the filter change:

(post_type = '$post_type_sql' OR post_type = 'attachment')

Perhaps we could allow for $post_types to be an array that gets imploded to build this segment?

comment:3 in reply to: ↑ 2 @SergeyBiryukov15 months ago

  • Keywords needs-patch added; reporter-feedback removed

Replying to zbtirrell:

Perhaps we could allow for $post_types to be an array that gets imploded to build this segment?

This seems like the way to go here. Let's do it for both get_page_by_path() and get_page_by_title().

@zbtirrell15 months ago

make the get_page_by_path function support an array of post types

comment:4 @zbtirrell15 months ago

I did not make a patch for get_page_by_title() because it does not suffer from the same difficulty. Since it simply does a query on title and post type, it does not consider the page's hierarchy/ancestry, so is not broken if a page has an parent that is not a page.

So, while extending that function to support an array would potentially be useful, it seems out of scope from this request.

Note: I tested the patch against trunk.

comment:5 @SergeyBiryukov15 months ago

  • Keywords has-patch added; needs-patch removed
  • Milestone changed from Awaiting Review to 3.9

comment:6 follow-up: @nacin14 months ago

We could choose to only add 'attachment' if you're passing a scalar value. If you pass an array, then it's an explicit list and attachments are ignored. Thoughts?

@zbtirrell14 months ago

a 3rd version of this patch, only adds attachment when post type is scalar

comment:7 in reply to: ↑ 6 @zbtirrell14 months ago

Replying to nacin:

We could choose to only add 'attachment' if you're passing a scalar value. If you pass an array, then it's an explicit list and attachments are ignored. Thoughts?

Seems like a great idea, updated patch has been uploaded.

comment:8 @nacin14 months ago

We're going to want to make the same changes to get_page_by_title() too.

@zbtirrell14 months ago

this patch also adds post type array support to get_page_by_title

comment:9 @zbtirrell14 months ago

The updated patch now contains a change to add post type array support to get_page_by_title() as requested.

comment:10 @johnbillion14 months ago

  • Owner set to johnbillion
  • Resolution set to fixed
  • Status changed from new to closed

In 27423:

Allow get_page_by_path() and get_page_by_title() to accept an array of post types. Fixes #24763. Props zbtirrell.

Note: See TracTickets for help on using tickets.