WordPress.org

Make WordPress Core

Opened 6 years ago

Last modified 5 months ago

#27094 new enhancement

get_boundary_post() doesn't work for custom post types

Reported by: PioneerSkies Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.8.1
Component: Query Keywords: has-patch
Focuses: Cc:
PR Number:

Description

Here the function

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)

27094-cpt.patch (2.7 KB) - added by tyxla 4 years ago.
Adding support for custom post types in get_boundary_post(), including unit tests
27094-cpt-with-page.patch (3.9 KB) - added by tyxla 4 years ago.
Adding support for custom post types and pages in get_boundary_post(), including unit tests

Download all attachments as: .zip

Change History (7)

#1 @SergeyBiryukov
6 years ago

Perhaps we should add a filter on $query_args, or make the function accept an associative array of arguments by checking func_num_args().

#2 @SergeyBiryukov
6 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 @wonderboymusic
5 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 @tyxla
4 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.

@tyxla
4 years ago

Adding support for custom post types in get_boundary_post(), including unit tests

@tyxla
4 years ago

Adding support for custom post types and pages in get_boundary_post(), including unit tests

#5 @tyxla
4 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 attachment post type - the function should return NULL for 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 page post type.
  • Contains a unit test for the custom post type use case.
  • Contains a unit test for the attachment post type - the function should return NULL for attachments.
  • Contains a unit test for the page post type.
Note: See TracTickets for help on using tickets.