- This topic is empty.
-
AuthorPosts
-
September 22, 2015 at 9:07 am #208536
Steven
ParticipantHowdy 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
September 23, 2015 at 7:02 am #208571Alen
ParticipantHey @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.
September 23, 2015 at 8:02 am #208575Steven
ParticipantThank 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.
September 23, 2015 at 9:00 am #208578Alen
ParticipantI’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.
September 23, 2015 at 12:13 pm #208585Steven
ParticipantWell 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!
September 23, 2015 at 2:42 pm #208590Steven
ParticipantI’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.
September 23, 2015 at 2:59 pm #208591Alen
ParticipantThat’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.
September 24, 2015 at 12:00 pm #208643Steven
ParticipantI have one follow up question for you.
Can I turn
$query->set()
into an array? I’d like to addorder
andorderby
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.
September 24, 2015 at 12:03 pm #208644Alen
ParticipantJust do
$query->set( 'posts_per_page', '-1' ); $query->set( 'orderby', 'name' ); $query->set( 'order', 'DESC' );September 24, 2015 at 12:40 pm #208653Steven
ParticipantAh. Thank you so much again :)
-
AuthorPosts
- The forum ‘Back End’ is closed to new topics and replies.