• Resolved Nikita_Sp

    (@nikitasp)


    Hi!

    I was trying to find out some info, but I couldn’t.
    Every answer use just:

    function limit_posts_per_archive_page($query) {
    	if ( is_post_type_archive('movie') && !is_admin() )
    		$query->set('posts_per_page', -1 );
    		// or
    		set_query_var('nopaging', true);
    		// or
    		set_query_var('posts_per_archive_page', $limit);
    	}	
    }
    add_filter('pre_get_posts', 'limit_posts_per_archive_page');

    But all of these solutions not working the way I want.

    The thing is the WordPress continue to process and return pagination pages even if there is -1 limit for the first page. So I get duplicate pages:
    example.com/movie/page/2
    example.com/movie/page/3

    example.com/movie/page/345

    And I can use ANY page number to 2147483647.

    One solution I’ve found seems to dirty… there must be a way to disable pagination clear. https://wordpress.stackexchange.com/questions/304137/disable-pagination-in-posts-and-pages

    Please, tell me what I’m doing wrong?
    Thanks.

    • This topic was modified 4 years, 11 months ago by Nikita_Sp.
Viewing 12 replies - 1 through 12 (of 12 total)
  • Moderator bcworkz

    (@bcworkz)

    Setting posts_per_page to -1 disables pagination so page number requests are ignored and you get the same content on every page. If you want all posts on one page, then the pagination links on the template should not be there so there would not be any page number requests.

    In any case, pagination links should not have page number links beyond 1 if all results are on that one page. When there is a discrepancy between total posts and page number links, it’s because a custom query was used without considering that pagination links are based on the main query and not the custom query.

    Thread Starter Nikita_Sp

    (@nikitasp)

    Hi @bcworkz, thanks for the response.

    The thing is there is no links to any pagination page (and it’s ok), BUT anyone can access to any page link. For example my competitor place a link on his website to page example.com/movie/page/123 and it will respond the SAME content as example.com/movie – this is not good for SEO at all.

    Also it’s strange – why it process this page if there is parameter -1? It should return 404.

    Thanks.

    Moderator bcworkz

    (@bcworkz)

    If inappropriate backlinks come from a limited set of domains, you can disavow backlinks in Google Search Console.

    Paged requests do not 404 because page parameters are ignored when the posts per page is -1. As far as WP is concerned, the query returned data so there is no reason to 404. WP does 404 when queries fail, not because URLs are incorrect. WP does try to fulfill requests even when URLs are not 100% correct. It’ll find close matches.

    It’s possible to cause paged requests to 404 if paged requests related to that query are always inappropriate. You can use the “pre_get_posts” action to set the query to go 404.

    Thread Starter Nikita_Sp

    (@nikitasp)

    @bcworkz yeah, I know about disallowing links, but the thing is the page exists, so I want to prevent it existing at all.

    So the final solution is to use following code:

    
    function limit_posts_per_archive_page($query) {
    	if ( is_post_type_archive('movie') && !is_admin() )
    		$query->set('posts_per_page', -1 );
    
    		$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    		if($paged > 1){
    			global $wp_query;
    			$wp_query->set_404();
    			status_header(404);
    		}
    	}	
    }
    add_filter('pre_get_posts', 'limit_posts_per_archive_page');
    

    Is it correct?

    Moderator bcworkz

    (@bcworkz)

    This would be better

    function limit_posts_per_archive_page( $query ) {
    	if ( $query->is_post_type_archive('movies') && !is_admin() ) {
    		$paged = ( $query->get('paged')) ? $query->get('paged') : 1;
    		if( $paged > 1){
    			$query->set_404();
    		} else {
    			$query->set('posts_per_page', -1 );
    		}
    	}
    }
    add_filter('pre_get_posts', 'limit_posts_per_archive_page');

    The principal difference being that you should use the passed object’s methods and not operate on the global $wp_query. (even though in essence you are doing so anyway). You don’t want to operate on the global query object when other post queries are being made.

    Thread Starter Nikita_Sp

    (@nikitasp)

    @bcworkz thanks you very much! One more question: what is more efficient and optimized:
    using set_query_var() function or directly through the $query object ($query->set())?

    Thanks.

    Moderator bcworkz

    (@bcworkz)

    Not enough difference to make a difference. Using the passed object’s set() method should be ever so slightly faster since the global object instance does not need to be retrieved. Additionally, passed object methods are the right way to handle things in a pre_get_posts callback.

    Thread Starter Nikita_Sp

    (@nikitasp)

    @bcworkz Thanks!

    • This reply was modified 4 years, 11 months ago by Nikita_Sp.
    Thread Starter Nikita_Sp

    (@nikitasp)

    @bcworkz sorry, but now (using $query->set) the parameters are set for every query on page (menu, etc…), and $query->is_main_query() is not working.

    Moderator bcworkz

    (@bcworkz)

    If main query check is not working the queries are probably all custom queries on a dedicated page template. If so, checking for paged logic should be done when establishing args the new WP_Query object.

    Was $query->is_post_type_archive('movies') not working?

    If all else fails, dump out the passed query objects and examine each ones properties for some distinct value that separates the target query from other ones.

    Thread Starter Nikita_Sp

    (@nikitasp)

    @bcworkz no, there is not any custom query, but everything seems to works fine now…
    maybe it was my mistake – some logical confusion (maybe with ! operand).

    Anyway, seems now everything is solved and fixed.
    Thank you very much for your time and help!

    Moderator bcworkz

    (@bcworkz)

    You’re welcome. It may have been caching confusing the result of any change. It’s always a good idea to flush any caches when you don’t get expected results.

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘WordPress process pagination pages on CPT archive with -1 limit’ is closed to new replies.