Skip to main content

Forums

This topic contains 9 replies, has 2 voices, and was last updated by  Steven 3 years, 9 months ago.

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #208536

    Steven
    Participant

    Howdy folks,

    I have an odd (hopefully simple) problem.

    I’d really like to change the number of posts per page on one of my custom post type’s archive pages. Thinking it should be super easy, I did a quick custom post type:

    $args = array(
      'post_type' => 'towns',
      'posts_per_page' => 10
    );
    $query = new WP_Query( 'args' );
    if( $query->have_posts() ) :
    ...
    

    But this returns “Nothing found”.

    Also, by default this archive page is returning 9 items per page. Why would it “randomly” decide to return 9 items on this CPT page when in my admin area I set blog posts to display 50 (just picked a wild number) items.

    I’ve tried this solution as well, but no luck: http://stackoverflow.com/questions/18968916/unlimited-posts-per-page-for-a-custom-post-type-only

    Any ideas?

    Thank you

    #208571

    Alen
    Participant

    Hey @steven,

    Whenever you’re working with Custom Post Types and your query is not returning anything, but you feel you did everything correct… go and reset the permalinks by visiting Settings >> Permalinks. Select Default press Save Changes, then switch it back to Post Name and press Save Changes again.

    In addition, you can modify your main query and just have your archive template return the results, no need for new WP_Query object.

    The answer is in the link you posted. I’ve modified it to fit what you’re trying to do. If you need it to return all posts set the value to -1:

    function set_posts_per_page_for_towns_cpt( $query ) {
      if ( !is_admin() && $query->is_main_query() && is_post_type_archive( 'towns' ) ) {
        $query->set( 'posts_per_page', '10' );
      }
    }
    add_action( 'pre_get_posts', 'set_posts_per_page_for_towns_cpt' );
    

    Place this code in your functions.php file.

    Read more about pre_get_posts.

    Hope that helps.

    #208575

    Steven
    Participant

    Thank you for your answer (and explanations), Alen.

    Sadly, it’s still not showing any more results on the archive page.

    I usually do the re-saving the permalinks “trick”, although I haven’t saved as default and then re-save as post-name. In any case, that hasn’t helped either :(

    Here’s a gist of my custom functions file and my archive file (in case there’s any weird conflict somewhere I’m not seeing): https://gist.github.com/swthate/0fbd3a449455c9ddbf1a

    If it’s any help, I’m using the FoundationPress theme as my starter theme.


    Edit:

    If my posts per page function looks a little different from the one you just posted, it’s because yours unfortunately wouldn’t work, so I tried the code from the StackOverflow answer again (even though they’re the same. ugh!)

    Thanks again.

    #208578

    Alen
    Participant

    I’ve tested your Gist code on fresh install of WordPress and default theme (15). And it works. I had to reset the Permalinks but it works.

    full size image

    #208585

    Steven
    Participant

    Well this is really weird.

    I switched my theme to Twenty Fifteen and copied over my functions for the custom post type and the posts per page, and it’s still only displaying just 9 posts per page.

    Right now I’m on my local setup, using MAMP and VirtualHost X. Could there be some sort of cache issue happening here? My apache server has been turned on and off a few times already, though, so I don’t feel like that could be it…

    I suppose I’ll have to try putting a quick fresh install together like you did, and hope its just a weird glitch that I wrote into the theme somewhere that I can bypass on a fresh install.

    Thanks!

    #208590

    Steven
    Participant

    I’ve confirmed that everything works on a clean install of WordPress and the FoundationPress theme as well. I’m going to take a crack at hunting down the culprit(s) before I go crazy and blow everything up.

    Thanks again for your help with this. :)

    EDIT:

    Found it! There was a conflict between my archive page and (I think) my “results” page from my Search & Filter Pro plugin. Temporarily deactivated that plugin, re-saved my perms, and now all my towns are listing!

    With this, I should be able to find a way to make them play nice together now.

    #208591

    Alen
    Participant

    That’s great. I was gonna suggest to check your trash bin and see if there’s posts or pages under the same slug towns, I have had issues with that as well in the past.

    Glad everything is working now.

    #208643

    Steven
    Participant

    I have one follow up question for you.

    Can I turn $query->set() into an array? I’d like to add order and orderby to it:

    function get_all_town_posts( $query ) {
      if( !is_admin() && $query->is_main_query() && is_post_type_archive( 'towns' ) ) {
        $query->set( array(
                'posts_per_page' => '-1',
                'orderby' => 'name',
                'order' => 'DESC'
            ));
      }
    }
    add_action( 'pre_get_posts', 'get_all_town_posts' );
    

    When I try that, it returns an error:

    Warning: Missing argument 2 for WP_Query::set(), called in /.../www/wp-content/themes/dst-fp/library/custom-functions.php on line 36 and defined in /.../www/wp-includes/query.php on line 2369
    
    Warning: Illegal offset type in /.../www/wp-includes/query.php on line 2370
    
    Notice: Undefined variable: value in /.../www/wp-includes/query.php on line 2370
    

    In the first error, line 36 would be line 8 in my first code example.

    #208644

    Alen
    Participant

    Just do


    $query->set( 'posts_per_page', '-1' ); $query->set( 'orderby', 'name' ); $query->set( 'order', 'DESC' );
    #208653

    Steven
    Participant

    Ah. Thank you so much again :)

Viewing 10 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic.

icon-link icon-logo-star icon-search icon-star