[wp-trac] [WordPress Trac] #15551: Custom post type pagination redirect

WordPress Trac noreply at wordpress.org
Tue Nov 15 00:19:33 UTC 2016


#15551: Custom post type pagination redirect
--------------------------+-----------------------------
 Reporter:  cbsad         |       Owner:
     Type:  defect (bug)  |      Status:  reopened
 Priority:  normal        |   Milestone:  Future Release
Component:  Query         |     Version:  3.1
 Severity:  normal        |  Resolution:
 Keywords:                |     Focuses:
--------------------------+-----------------------------

Comment (by elvismdev):

 Use `in_array` http://php.net/manual/en/function.in-array.php

 Your example will look like:

 {{{#!php
 <?php
 function fix_request_redirect( $request ) {

         $post_types = array(
                 'place',
                 'org',
                 'label',
                 'person',
                 'company',
                 'fest',
                 'release'
                 );

         if ( isset( $request->query_vars['post_type'] )
                 && in_array( $request->query_vars['post_type'],
 $post_types )
                 && true === $request->is_singular
                 && - 1 == $request->current_post
                 && true === $request->is_paged
                 )
         {
                 add_filter( 'redirect_canonical', '__return_false' );
         }

         return $request;
 }
 add_action( 'parse_query', 'fix_request_redirect' );
 }}}


 Replying to [comment:19 yuranikolaev]:
 > Can someone explain, how to make it work with an array of post types?
 > For example, we got a few post types - band, person, release, cinema,
 place etc...
 >
 > I tried this one
 >
 > {{{#!php
 > <?php
 > function fix_request_redirect( $request ) {
 >
 >         if ( isset( $request->query_vars['post_type'] )
 >              && 'place' === $request->query_vars['post_type'] OR 'org'
 === $request->query_vars['post_type'] OR 'label' ===
 $request->query_vars['post_type'] OR 'band' ===
 $request->query_vars['post_type'] OR 'person' ===
 $request->query_vars['post_type'] OR 'company' ===
 $request->query_vars['post_type'] OR 'fest' ===
 $request->query_vars['post_type'] OR 'release' ===
 $request->query_vars['post_type']
 >              && true === $request->is_singular
 >              && - 1 == $request->current_post
 >              && true === $request->is_paged
 >         ) {
 >                 add_filter( 'redirect_canonical', '__return_false' );
 >         }
 >
 >         return $request;
 > }
 >
 > add_action( 'parse_query', 'fix_request_redirect' );
 > }}}
 >
 > and it works!
 > But it also make errors in php logs, and, I think, not so perfect
 >
 > I also playing with arrays, but cannot figure how to make it work
 properly
 >
 > Is it possible at all?
 >
 > Thanks in advance

--
Ticket URL: <https://core.trac.wordpress.org/ticket/15551#comment:20>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list