WordPress.org

Make WordPress Core

Opened 2 years ago

Last modified 3 months ago

#32322 assigned defect (bug)

Custom permalink structure incorrect for Future posts

Reported by: greencode Owned by: kdoole
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 4.2
Component: Permalinks Keywords: good-first-bug needs-unit-tests has-patch
Focuses: Cc:

Description

From V4.2 there is a bug with custom permalink structures used for Future posts that are visible on the front end.

Example:

  • You have your custom permalink structure set up as "Post name".
  • "Published" posts show up with the correct permalink structure i.e. mysite.com/single-post/
  • "posts marked as "Future" but visible in the front end (i.e. if you're wanting to show posts that are scheduled) show up with the default permalink structure i.e. mysite.com/?p=618

Rolling back through the versions of WordPress, all was working correctly in v4.1.5 and then stopped working from v4.2

Attachments (3)

link-template.diff (1.1 KB) - added by kdoole 2 years ago.
adds a filter to the list of post types which should not receive fancy permalinks
link-template-2.diff (2.5 KB) - added by kdoole 2 years ago.
32322-unit-tests.diff (1.5 KB) - added by kdoole 2 years ago.

Download all attachments as: .zip

Change History (20)

#1 @greencode
2 years ago

  • Summary changed from Default permalink structure showing for Future posts to Custom permalink structure incorrect for Future posts

#2 @SergeyBiryukov
2 years ago

Hi @greencode, welcome to Trac!

Looks like this was an intended change in #30910.

#3 follow-up: @greencode
2 years ago

Thanks for the quick reply. I've had a look at the link you sent but and whilst that appears to be exactly the issue I'm talking about, I'm unsure how to rectify the issue so that the pretty permalink is shown instead of the default permalink. I'm using

<?php the_permalink(); ?>

to generate the link.

#4 in reply to: ↑ 3 @rob.page
2 years ago

Hi, I'm in the same boat as @greencode. I'm unclear how to modify our pages to workaround this change. We have an events page that is now showing the numeric permalink for future events, both as a result of the_permalink() as well as the {PERMALINK} shortcode used in a widget.

How should I be modifying our pages to show the "pretty" permalink?

Thanks.

Replying to greencode:

Thanks for the quick reply. I've had a look at the link you sent but and whilst that appears to be exactly the issue I'm talking about, I'm unsure how to rectify the issue so that the pretty permalink is shown instead of the default permalink. I'm using

<?php the_permalink(); ?>

to generate the link.

#5 follow-up: @johnbillion
2 years ago

  • Keywords needs-patch good-first-bug needs-unit-tests added
  • Type changed from defect (bug) to enhancement

There isn't a simple way to do it. Maybe we need a filter around the array of post statuses in the get_permalink() function to control which statuses get a pretty permalink.

#6 in reply to: ↑ 5 @kdoole
2 years ago

Howdy,

@johnbillion, you made it too easy! :) Here's an initial patch, just to see how this looks. Hope i'm doing this right...

I swapped the if and else here -- it seemed to make sense asking "should we use a short link" instead of "should we not use a fancy link."

Besides the several opportunities in get_permalink to clean up the code style, how's this look?

@kdoole
2 years ago

adds a filter to the list of post types which should not receive fancy permalinks

#7 @kreatif
2 years ago

  • Type changed from enhancement to defect (bug)

I have the same problem with the post status,
i updated the link-template with the patch above but this is nog working for me.

For the post i'm using custom post type ui plugin, needs this a other patch to fix it ?

#8 @kdoole
2 years ago

hi @kreatif,

With this patch, you would need to add a filter to your theme. Something like,

add_filter( 'shortlink_post_statuses', 'prettylink_future_posts' );
function prettylink_future_posts( $post_statuses ) {
	$future_index = array_search( 'future', $post_statuses );
	unset( $post_statuses[ $future_index ] );
	return $post_statuses;
}

#9 @johnbillion
2 years ago

  • Keywords has-patch added; needs-patch removed

This is probably the best approach.

The filter will need inline filter documentation and some more unit tests along with those in the Tests_Link class.

#10 @kdoole
2 years ago

  • Added inline filter docs.
  • Added the filter to get_post_permalink, which i missed at first.
  • I also spent a moment creating unit tests; it feels a bit overly verbose -- is this making sense?

On that note, there's a bit of duplication going on in here, this piece:

$statuses_which_should_shortlink = apply_filters( 'shortlink_post_statuses', array(
	'draft',
	'pending',
	'auto-draft',
	'future'
) );

It was already duplicated, and now with the filter it's a touch longer. Does it make any sense pulling this out into a function?

#11 @kreatif
2 years ago

@kdool, Thanks, works like a charm
I've one bug that is related to this, but don't know if this is a wordpress bug or custom post type ui bug;

When i click on a post i get a blank page (with post status => furure).
Two other post types with post status => published dont't have this problem

Error that i get: Failed to load resource: the server responded with a status of 404 (Not Found)
When i'm visiting the post via edit post -> view post, i can see the post

#12 @kdoole
2 years ago

@kreatif, formatting the permalink is one thing, but enabling non-logged in users to view future posts is another. I did a very quick search and found this plugin, which seems to work by doing the following:

add_filter( 'the_posts', 'show_future_posts' );
function show_future_posts( $posts ) {
   global $wp_query, $wpdb;

   if ( is_single() && $wp_query->post_count == 0 ) {
      $posts = $wpdb->get_results( $wp_query->request );
   }

   return $posts;
}

#13 @kreatif
2 years ago

  • Resolution set to worksforme
  • Status changed from new to closed

@kdoole Thanks the add filter code solved the problem.
I didn't kwow about the non-logged users can see the future posts.
Learned something new, Thanks!

#14 @DrewAPicture
2 years ago

  • Resolution worksforme deleted
  • Status changed from closed to reopened

#15 @obenland
2 years ago

  • Owner set to kdoole
  • Status changed from reopened to assigned

#16 @johnbillion
2 years ago

There's further inconsistency here. For a scheduled post, the permalink used in various places is inconsistent.

  • On the post listing screen, the 'View' link looks like example.com?p=123&preview=true
  • On the post editing screen, the 'View Post' link in the admin toolbar looks like example.com?p=123
  • On the post editing screen, the 'View Post' button next to the slug editor is the pretty permalink

Whichever URL you view the post at, the is_preview() conditional is true, so the non-pretty links should always be in the format example.com?p=123&preview=true.

#17 in reply to: ↑ description @bryancave
3 months ago

Is there any progress on this? Still seems to be an issue.

Note: See TracTickets for help on using tickets.