Opened 12 years ago
Last modified 7 years ago
#27094 new enhancement
get_boundary_post() doesn't work for custom post types
| Reported by: |
|
Owned by: | |
|---|---|---|---|
| Milestone: | Priority: | normal | |
| Severity: | normal | Version: | 3.8.1 |
| Component: | Query | Keywords: | has-patch |
| Focuses: | Cc: |
Description
I'd suggest a very little change in order to open use of the function to CPTs:
function custom_get_boundary_post( $in_same_term = false, $excluded_terms = '', $start = true, $taxonomy = 'category', $post_type = 'post' ) {
$post = get_post();
if ( ! $post || ! is_single() || is_attachment() || ! taxonomy_exists( $taxonomy ) )
return null;
$query_args = array(
'post_type' => $post_type,
'posts_per_page' => 1,
'order' => $start ? 'ASC' : 'DESC',
);
[snip]
The order of $taxonomy and $post_type arguments isn't ideal imho, but would be absolutely backward compatible.
Attachments (2)
Change History (7)
#2
@
12 years ago
A workaround:
function set_post_type_for_get_boundary_post_27094( $query ) {
$query->set( 'post_type', 'page' );
}
...
add_action( 'pre_get_posts', 'set_post_type_for_get_boundary_post_27094' );
$post = get_boundary_post();
remove_action( 'pre_get_posts', 'set_post_type_for_get_boundary_post_27094' );
#3
@
12 years ago
- Keywords needs-patch needs-unit-tests added
- Milestone changed from Awaiting Review to Future Release
We should add get_boundary_post_for_type( $type, $args ) or something in lieu of adding a 5th (Fifth) arg to get_boundary_post()
#4
@
10 years ago
I don't see a particular reason to have to specify the post type. This function relies on the global $post and $wp_query anyway (it will return NULL if there is no post in the current query, or if the current post is a page or an attachment), so I believe the best way to achieve this is to use the post type of the current post.
@
10 years ago
Adding support for custom post types and pages in get_boundary_post(), including unit tests
#5
@
10 years ago
- Keywords has-patch added; needs-patch needs-unit-tests removed
I've just added 2 patches, because I am not sure if this function should also support pages (the workaround from @SergeyBiryukov makes me think that pages should also be supported).
Both patches take advantage of the current post and use its post type, so there is no need for a new function or a function argument.
27094-cpt.patch
- Adds support for custom post types, fetches the post type automatically from the current post.
- Contains a unit test for the custom post type use case.
- Contains a unit test for the
attachmentpost type - the function should returnNULLfor attachments.
27094-cpt-with-page.patch
- Adds support for custom post types, fetches the post type automatically from the current post.
- Adds support for the
pagepost type. - Contains a unit test for the custom post type use case.
- Contains a unit test for the
attachmentpost type - the function should returnNULLfor attachments. - Contains a unit test for the
pagepost type.
Perhaps we should add a filter on
$query_args, or make the function accept an associative array of arguments by checkingfunc_num_args().