WordPress.org

Ready to get started?Download WordPress

Forums

pre_get_posts doesn't work with pretty permalinks anymore (16 posts)

  1. cestbibi
    Member
    Posted 7 months ago #

    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 ?

  2. bcworkz
    Member
    Posted 7 months ago #

    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.

  3. cestbibi
    Member
    Posted 6 months ago #

    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.

  4. imfromio
    Member
    Posted 6 months ago #

    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?

  5. Samuel Wood (Otto)
    Tech Ninja
    Posted 6 months ago #

    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.

  6. cestbibi
    Member
    Posted 6 months ago #

    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.

  7. imfromio
    Member
    Posted 6 months ago #

    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.

  8. Samuel Wood (Otto)
    Tech Ninja
    Posted 6 months ago #

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

  9. cestbibi
    Member
    Posted 6 months ago #

    Great! thanks a lot @Otto

  10. Samuel Wood (Otto)
    Tech Ninja
    Posted 6 months ago #

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

    http://core.trac.wordpress.org/ticket/26627

  11. cestbibi
    Member
    Posted 6 months ago #

    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 ..

  12. bcworkz
    Member
    Posted 6 months ago #

    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.

  13. cestbibi
    Member
    Posted 6 months ago #

    Thanks @bcworkz!

  14. Helman Jr
    Member
    Posted 6 months ago #

    jeremyfelt patch works for me 26627.2.diff

    As I am not showing some subcategories posts in parent category archives,
    and on homepage.

    Everything ('pre_get_posts') was working ok untill I just updated to wordpress3.8 yesterday.

    But now again I just updated wp-includes/query.php and it works as before.

  15. cestbibi
    Member
    Posted 6 months ago #

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

  16. imfromio
    Member
    Posted 6 months ago #

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

Reply

You must log in to post.

About this Topic