• Hi,
    I have this in my functions.php to limit number of posts displayed for the “retrats” category :

    function hwl_home_pagesize( $query ) {
        if ( is_admin() || ! $query->is_main_query() )
            return;
    
        if ( is_category('retrats') ) {
            $query->set( 'posts_per_page', 1 );
            return;
        }
    }
    add_action( 'pre_get_posts', 'hwl_home_pagesize', 1 );

    Since upgrade to 3.8 that doesn’t work anymore with pretty permalinks, only in default ones (then it works)
    Someone noticed that already ?

Viewing 14 replies - 1 through 14 (of 14 total)
  • Moderator bcworkz

    (@bcworkz)

    Have you tried setting ‘posts_per_archive_page’ instead? A category page is a type of archive page after all. Just a wild guess, but worth a try.

    I’ve no idea why permalinks would have anything to do with this, I guess some different logic is applied when processing the query.

    Thread Starter cestbibi

    (@cestbibi)

    Thankyou @bcworkz that didn′t work.
    I want to stress that the code worked ok with previous wordpress versions (as 3.7.1) and stopped working after the upgrade.

    I am having a similar problem after the upgrade to 3.8. I have a certain category of posts (id=263) that I keep separate from the others. In my functions.php file I have this code:

    function tcm_pre_get_posts( $query ) {
        if ( is_admin() || is_author() || is_search() || ! $query->is_main_query() )
            return;
    
        if ( !is_category( 263 ) ) {
            $query->set( 'cat', -263 );
            return;
        } else {
    		$query->set( 'meta_key', '_thumbnail_id' );
    		$query->set( 'orderby', 'meta_value' );
    		$query->set( 'orderby', 'date' );
            return;
    	}
    }
    add_action( 'pre_get_posts', 'tcm_pre_get_posts');

    The code still works to keep id=263 items out of the main posts page, but the page that is supposed to show id=263 posts no longer returns any posts. This code worked fine before the update to WP 3.8. Has something been updated with the pre_get_posts function?

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.ads-software.com Admin

    Instead of calling is_category(whatever), try calling $query->is_category(whatever). Same goes for any of the other query conditions, like is_author or is_search.

    Calling the base is_category() function is always going to check the main page query. But because you’re filtering on pre_get_posts, you actually want to check what the settings are for the query that it’s passing you.

    So cestbibi’s code should be this:

    function hwl_home_pagesize( $query ) {
        if ( is_admin() || ! $query->is_main_query() )
            return;
    
        if ( $query->is_category('retrats') ) {
            $query->set( 'posts_per_page', 1 );
            return;
        }
    }
    add_action( 'pre_get_posts', 'hwl_home_pagesize', 1 );

    And imfromio’s code should be this:

    function tcm_pre_get_posts( $query ) {
        if ( is_admin() || $query->is_author() || $query->is_search() || ! $query->is_main_query() )
            return;
    
        if ( !$query->is_category( 263 ) ) {
            $query->set( 'cat', -263 );
            return;
        } else {
    		$query->set( 'meta_key', '_thumbnail_id' );
    		$query->set( 'orderby', 'meta_value' );
    		$query->set( 'orderby', 'date' );
            return;
    	}
    }
    add_action( 'pre_get_posts', 'tcm_pre_get_posts');

    The fact that it worked for you previously was probably a side effect of how or where you were calling it before. It should not have worked that way, you should always be only checking against the passed in $query in pre_get_posts calls.

    Thread Starter cestbibi

    (@cestbibi)

    Thank you @otto but that didn’t work either.
    I think it’s something that has to do with the permalinks because as I said previously the code actually works but ONLY with the Default setting permalink structure.
    I didn’t alter anything in the code, it just was working in 3.7.1 and stopped working suddenly after 3.8 upgrade.

    Thanks Otto but that did not solve my problem either. Still not see any posts on the catgeory 263 page. I will look into my permalinks and see if that may have something to do with it.

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.ads-software.com Admin

    You’re right. I tested it, and this is broken. I’ll file a bug report about it.

    Thread Starter cestbibi

    (@cestbibi)

    Great! thanks a lot @otto

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.ads-software.com Admin

    I filed a report on the problem with some test code. Hopefully we can get a fix soon.

    https://core.trac.www.ads-software.com/ticket/26627

    Thread Starter cestbibi

    (@cestbibi)

    Just a quick thought : wouldn’t it be cool to have something like jsfiddle to replicate / test those functions ?? Would help you guys a lot that give us support ..

    Moderator bcworkz

    (@bcworkz)

    It would be cool! It would help resolve the issue of unintended plugin influence by offering a common ground.

    BTW, a patch has already been offered but needs testing. It’s small enough to apply manually if you don’t have terminal access. See Otto’s link.

    Thread Starter cestbibi

    (@cestbibi)

    Thanks @bcworkz!

    Thread Starter cestbibi

    (@cestbibi)

    Applied patch, works for me.
    Thankyou guys I’m always amazed by this developers community, you are so proactive ??

    The patch worked for me as well. Fantastic response – thanks!

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘pre_get_posts doesn't work with pretty permalinks anymore’ is closed to new replies.