Is it possible to remove the posts_per_page limit on a specific post type?
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
2
down vote
favorite
I'd like to know if I can remove the limit on the posts_per_page for a specific post type.
In the archive.php page I'm displaying different post type, and for the specific "publications" post type I want to display all the posts. How can I achieve this without impacting the traditional "post" type?
custom-post-types custom-post-type-archives
New contributor
VeeJay is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |Â
up vote
2
down vote
favorite
I'd like to know if I can remove the limit on the posts_per_page for a specific post type.
In the archive.php page I'm displaying different post type, and for the specific "publications" post type I want to display all the posts. How can I achieve this without impacting the traditional "post" type?
custom-post-types custom-post-type-archives
New contributor
VeeJay is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I'd like to know if I can remove the limit on the posts_per_page for a specific post type.
In the archive.php page I'm displaying different post type, and for the specific "publications" post type I want to display all the posts. How can I achieve this without impacting the traditional "post" type?
custom-post-types custom-post-type-archives
New contributor
VeeJay is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I'd like to know if I can remove the limit on the posts_per_page for a specific post type.
In the archive.php page I'm displaying different post type, and for the specific "publications" post type I want to display all the posts. How can I achieve this without impacting the traditional "post" type?
custom-post-types custom-post-type-archives
custom-post-types custom-post-type-archives
New contributor
VeeJay is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
VeeJay is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
VeeJay is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 4 hours ago
VeeJay
111
111
New contributor
VeeJay is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
VeeJay is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
VeeJay is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |Â
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
2
down vote
You can hook into the pre_get_posts
action, to access the $query
object.
You should use your action hook inside your functions.php.
An example of what your function could look like:
add_action( 'pre_get_posts', 'publications_archive_query' );
function publications_archive_query( $query ) {
if ( !is_admin() && $query->is_main_query())
if ( $query->get('post_type') === 'publications' )
$query->set( 'posts_per_page', 5 );
Narrow down what is the query you are modifying by using conditional checks.
$query->get('post_type')
to get current's Query post_type to check against.
is_main_query
to make sure you are only applying your query modification to the main query.
Read further and find more examples:
https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts
1
I’m afraid it won’t work. You use get_post_type to check post type. But this function checks post type of global $post variable. And during pre_get_post that variable is not set yet ;)
– Krzysiek Dróżdż
1 hour ago
1
@KrzysiekDróżdż - yes you are right. Thanks for the note. I am editing the answer and also fixed a syntax error in my first if ().
– FFrewin
44 mins ago
1
Still not correct. 1. You still write about get_post_type. 2. post_type doesn't have to be set, so your can cause notices and warnings.
– Krzysiek Dróżdż
17 mins ago
@KrzysiekDróżdż: yes - I only updated my code ... forgot to correct the rest of my answer. Thanks! Fixed now (hope I didn't miss anything else this time)
– FFrewin
12 mins ago
add a comment |Â
up vote
-1
down vote
If you are creating the query yourself inside those .php
pages, then it's better to pass -1 in argument to obtain unlimited posts:
$args = array( ...... 'posts_per_page'=>-1, .....)
$your_query = new WP_Query($args);
If you dont have access to the query-creating .php
file, then you might neeed to use less-recommended version (which affects all pages/queries with that custom-type), like @FFrewin suggested.
Well, modifying global wp_query usingpre_get_posts
is not less-recommended. You always should use global wp_query and modify it with filters, if it's only possible and not create your own queries.
– Krzysiek Dróżdż
13 mins ago
@KrzysiekDróżdż I neither way agree with you and it's really "less-recommended" to modify the global query, and what's more no indication which pages you are affecting (and that affects any existing page withcpt
publications
). Instead, it's better to make a custom query (instead of altering whole site) within the targeted.php
. However, you have right to downvote my question (with incorrect reason), and ok, it's your right.
– T.Todua
9 mins ago
Creating a new WP_Query in this context leads to querying the database twice. As you have a second query over the main one.
– FFrewin
7 mins ago
@FFrewin to say, actually i dont see any problem with that. You are not re-creating new queries on all pages, instead doing on sepcific pages, and even i dont think you have million of hits in hour on that page, to worry about DB overload (which is really good habbit to worry about overload and always try to have optimized code). But, in this case i dont see real problem. ok, i just suggested my answer, you decide.
– T.Todua
4 mins ago
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
You can hook into the pre_get_posts
action, to access the $query
object.
You should use your action hook inside your functions.php.
An example of what your function could look like:
add_action( 'pre_get_posts', 'publications_archive_query' );
function publications_archive_query( $query ) {
if ( !is_admin() && $query->is_main_query())
if ( $query->get('post_type') === 'publications' )
$query->set( 'posts_per_page', 5 );
Narrow down what is the query you are modifying by using conditional checks.
$query->get('post_type')
to get current's Query post_type to check against.
is_main_query
to make sure you are only applying your query modification to the main query.
Read further and find more examples:
https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts
1
I’m afraid it won’t work. You use get_post_type to check post type. But this function checks post type of global $post variable. And during pre_get_post that variable is not set yet ;)
– Krzysiek Dróżdż
1 hour ago
1
@KrzysiekDróżdż - yes you are right. Thanks for the note. I am editing the answer and also fixed a syntax error in my first if ().
– FFrewin
44 mins ago
1
Still not correct. 1. You still write about get_post_type. 2. post_type doesn't have to be set, so your can cause notices and warnings.
– Krzysiek Dróżdż
17 mins ago
@KrzysiekDróżdż: yes - I only updated my code ... forgot to correct the rest of my answer. Thanks! Fixed now (hope I didn't miss anything else this time)
– FFrewin
12 mins ago
add a comment |Â
up vote
2
down vote
You can hook into the pre_get_posts
action, to access the $query
object.
You should use your action hook inside your functions.php.
An example of what your function could look like:
add_action( 'pre_get_posts', 'publications_archive_query' );
function publications_archive_query( $query ) {
if ( !is_admin() && $query->is_main_query())
if ( $query->get('post_type') === 'publications' )
$query->set( 'posts_per_page', 5 );
Narrow down what is the query you are modifying by using conditional checks.
$query->get('post_type')
to get current's Query post_type to check against.
is_main_query
to make sure you are only applying your query modification to the main query.
Read further and find more examples:
https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts
1
I’m afraid it won’t work. You use get_post_type to check post type. But this function checks post type of global $post variable. And during pre_get_post that variable is not set yet ;)
– Krzysiek Dróżdż
1 hour ago
1
@KrzysiekDróżdż - yes you are right. Thanks for the note. I am editing the answer and also fixed a syntax error in my first if ().
– FFrewin
44 mins ago
1
Still not correct. 1. You still write about get_post_type. 2. post_type doesn't have to be set, so your can cause notices and warnings.
– Krzysiek Dróżdż
17 mins ago
@KrzysiekDróżdż: yes - I only updated my code ... forgot to correct the rest of my answer. Thanks! Fixed now (hope I didn't miss anything else this time)
– FFrewin
12 mins ago
add a comment |Â
up vote
2
down vote
up vote
2
down vote
You can hook into the pre_get_posts
action, to access the $query
object.
You should use your action hook inside your functions.php.
An example of what your function could look like:
add_action( 'pre_get_posts', 'publications_archive_query' );
function publications_archive_query( $query ) {
if ( !is_admin() && $query->is_main_query())
if ( $query->get('post_type') === 'publications' )
$query->set( 'posts_per_page', 5 );
Narrow down what is the query you are modifying by using conditional checks.
$query->get('post_type')
to get current's Query post_type to check against.
is_main_query
to make sure you are only applying your query modification to the main query.
Read further and find more examples:
https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts
You can hook into the pre_get_posts
action, to access the $query
object.
You should use your action hook inside your functions.php.
An example of what your function could look like:
add_action( 'pre_get_posts', 'publications_archive_query' );
function publications_archive_query( $query ) {
if ( !is_admin() && $query->is_main_query())
if ( $query->get('post_type') === 'publications' )
$query->set( 'posts_per_page', 5 );
Narrow down what is the query you are modifying by using conditional checks.
$query->get('post_type')
to get current's Query post_type to check against.
is_main_query
to make sure you are only applying your query modification to the main query.
Read further and find more examples:
https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts
edited 11 mins ago
answered 3 hours ago
FFrewin
1357
1357
1
I’m afraid it won’t work. You use get_post_type to check post type. But this function checks post type of global $post variable. And during pre_get_post that variable is not set yet ;)
– Krzysiek Dróżdż
1 hour ago
1
@KrzysiekDróżdż - yes you are right. Thanks for the note. I am editing the answer and also fixed a syntax error in my first if ().
– FFrewin
44 mins ago
1
Still not correct. 1. You still write about get_post_type. 2. post_type doesn't have to be set, so your can cause notices and warnings.
– Krzysiek Dróżdż
17 mins ago
@KrzysiekDróżdż: yes - I only updated my code ... forgot to correct the rest of my answer. Thanks! Fixed now (hope I didn't miss anything else this time)
– FFrewin
12 mins ago
add a comment |Â
1
I’m afraid it won’t work. You use get_post_type to check post type. But this function checks post type of global $post variable. And during pre_get_post that variable is not set yet ;)
– Krzysiek Dróżdż
1 hour ago
1
@KrzysiekDróżdż - yes you are right. Thanks for the note. I am editing the answer and also fixed a syntax error in my first if ().
– FFrewin
44 mins ago
1
Still not correct. 1. You still write about get_post_type. 2. post_type doesn't have to be set, so your can cause notices and warnings.
– Krzysiek Dróżdż
17 mins ago
@KrzysiekDróżdż: yes - I only updated my code ... forgot to correct the rest of my answer. Thanks! Fixed now (hope I didn't miss anything else this time)
– FFrewin
12 mins ago
1
1
I’m afraid it won’t work. You use get_post_type to check post type. But this function checks post type of global $post variable. And during pre_get_post that variable is not set yet ;)
– Krzysiek Dróżdż
1 hour ago
I’m afraid it won’t work. You use get_post_type to check post type. But this function checks post type of global $post variable. And during pre_get_post that variable is not set yet ;)
– Krzysiek Dróżdż
1 hour ago
1
1
@KrzysiekDróżdż - yes you are right. Thanks for the note. I am editing the answer and also fixed a syntax error in my first if ().
– FFrewin
44 mins ago
@KrzysiekDróżdż - yes you are right. Thanks for the note. I am editing the answer and also fixed a syntax error in my first if ().
– FFrewin
44 mins ago
1
1
Still not correct. 1. You still write about get_post_type. 2. post_type doesn't have to be set, so your can cause notices and warnings.
– Krzysiek Dróżdż
17 mins ago
Still not correct. 1. You still write about get_post_type. 2. post_type doesn't have to be set, so your can cause notices and warnings.
– Krzysiek Dróżdż
17 mins ago
@KrzysiekDróżdż: yes - I only updated my code ... forgot to correct the rest of my answer. Thanks! Fixed now (hope I didn't miss anything else this time)
– FFrewin
12 mins ago
@KrzysiekDróżdż: yes - I only updated my code ... forgot to correct the rest of my answer. Thanks! Fixed now (hope I didn't miss anything else this time)
– FFrewin
12 mins ago
add a comment |Â
up vote
-1
down vote
If you are creating the query yourself inside those .php
pages, then it's better to pass -1 in argument to obtain unlimited posts:
$args = array( ...... 'posts_per_page'=>-1, .....)
$your_query = new WP_Query($args);
If you dont have access to the query-creating .php
file, then you might neeed to use less-recommended version (which affects all pages/queries with that custom-type), like @FFrewin suggested.
Well, modifying global wp_query usingpre_get_posts
is not less-recommended. You always should use global wp_query and modify it with filters, if it's only possible and not create your own queries.
– Krzysiek Dróżdż
13 mins ago
@KrzysiekDróżdż I neither way agree with you and it's really "less-recommended" to modify the global query, and what's more no indication which pages you are affecting (and that affects any existing page withcpt
publications
). Instead, it's better to make a custom query (instead of altering whole site) within the targeted.php
. However, you have right to downvote my question (with incorrect reason), and ok, it's your right.
– T.Todua
9 mins ago
Creating a new WP_Query in this context leads to querying the database twice. As you have a second query over the main one.
– FFrewin
7 mins ago
@FFrewin to say, actually i dont see any problem with that. You are not re-creating new queries on all pages, instead doing on sepcific pages, and even i dont think you have million of hits in hour on that page, to worry about DB overload (which is really good habbit to worry about overload and always try to have optimized code). But, in this case i dont see real problem. ok, i just suggested my answer, you decide.
– T.Todua
4 mins ago
add a comment |Â
up vote
-1
down vote
If you are creating the query yourself inside those .php
pages, then it's better to pass -1 in argument to obtain unlimited posts:
$args = array( ...... 'posts_per_page'=>-1, .....)
$your_query = new WP_Query($args);
If you dont have access to the query-creating .php
file, then you might neeed to use less-recommended version (which affects all pages/queries with that custom-type), like @FFrewin suggested.
Well, modifying global wp_query usingpre_get_posts
is not less-recommended. You always should use global wp_query and modify it with filters, if it's only possible and not create your own queries.
– Krzysiek Dróżdż
13 mins ago
@KrzysiekDróżdż I neither way agree with you and it's really "less-recommended" to modify the global query, and what's more no indication which pages you are affecting (and that affects any existing page withcpt
publications
). Instead, it's better to make a custom query (instead of altering whole site) within the targeted.php
. However, you have right to downvote my question (with incorrect reason), and ok, it's your right.
– T.Todua
9 mins ago
Creating a new WP_Query in this context leads to querying the database twice. As you have a second query over the main one.
– FFrewin
7 mins ago
@FFrewin to say, actually i dont see any problem with that. You are not re-creating new queries on all pages, instead doing on sepcific pages, and even i dont think you have million of hits in hour on that page, to worry about DB overload (which is really good habbit to worry about overload and always try to have optimized code). But, in this case i dont see real problem. ok, i just suggested my answer, you decide.
– T.Todua
4 mins ago
add a comment |Â
up vote
-1
down vote
up vote
-1
down vote
If you are creating the query yourself inside those .php
pages, then it's better to pass -1 in argument to obtain unlimited posts:
$args = array( ...... 'posts_per_page'=>-1, .....)
$your_query = new WP_Query($args);
If you dont have access to the query-creating .php
file, then you might neeed to use less-recommended version (which affects all pages/queries with that custom-type), like @FFrewin suggested.
If you are creating the query yourself inside those .php
pages, then it's better to pass -1 in argument to obtain unlimited posts:
$args = array( ...... 'posts_per_page'=>-1, .....)
$your_query = new WP_Query($args);
If you dont have access to the query-creating .php
file, then you might neeed to use less-recommended version (which affects all pages/queries with that custom-type), like @FFrewin suggested.
edited 7 mins ago
answered 19 mins ago


T.Todua
2,15852146
2,15852146
Well, modifying global wp_query usingpre_get_posts
is not less-recommended. You always should use global wp_query and modify it with filters, if it's only possible and not create your own queries.
– Krzysiek Dróżdż
13 mins ago
@KrzysiekDróżdż I neither way agree with you and it's really "less-recommended" to modify the global query, and what's more no indication which pages you are affecting (and that affects any existing page withcpt
publications
). Instead, it's better to make a custom query (instead of altering whole site) within the targeted.php
. However, you have right to downvote my question (with incorrect reason), and ok, it's your right.
– T.Todua
9 mins ago
Creating a new WP_Query in this context leads to querying the database twice. As you have a second query over the main one.
– FFrewin
7 mins ago
@FFrewin to say, actually i dont see any problem with that. You are not re-creating new queries on all pages, instead doing on sepcific pages, and even i dont think you have million of hits in hour on that page, to worry about DB overload (which is really good habbit to worry about overload and always try to have optimized code). But, in this case i dont see real problem. ok, i just suggested my answer, you decide.
– T.Todua
4 mins ago
add a comment |Â
Well, modifying global wp_query usingpre_get_posts
is not less-recommended. You always should use global wp_query and modify it with filters, if it's only possible and not create your own queries.
– Krzysiek Dróżdż
13 mins ago
@KrzysiekDróżdż I neither way agree with you and it's really "less-recommended" to modify the global query, and what's more no indication which pages you are affecting (and that affects any existing page withcpt
publications
). Instead, it's better to make a custom query (instead of altering whole site) within the targeted.php
. However, you have right to downvote my question (with incorrect reason), and ok, it's your right.
– T.Todua
9 mins ago
Creating a new WP_Query in this context leads to querying the database twice. As you have a second query over the main one.
– FFrewin
7 mins ago
@FFrewin to say, actually i dont see any problem with that. You are not re-creating new queries on all pages, instead doing on sepcific pages, and even i dont think you have million of hits in hour on that page, to worry about DB overload (which is really good habbit to worry about overload and always try to have optimized code). But, in this case i dont see real problem. ok, i just suggested my answer, you decide.
– T.Todua
4 mins ago
Well, modifying global wp_query using
pre_get_posts
is not less-recommended. You always should use global wp_query and modify it with filters, if it's only possible and not create your own queries.– Krzysiek Dróżdż
13 mins ago
Well, modifying global wp_query using
pre_get_posts
is not less-recommended. You always should use global wp_query and modify it with filters, if it's only possible and not create your own queries.– Krzysiek Dróżdż
13 mins ago
@KrzysiekDróżdż I neither way agree with you and it's really "less-recommended" to modify the global query, and what's more no indication which pages you are affecting (and that affects any existing page with
cpt
publications
). Instead, it's better to make a custom query (instead of altering whole site) within the targeted .php
. However, you have right to downvote my question (with incorrect reason), and ok, it's your right.– T.Todua
9 mins ago
@KrzysiekDróżdż I neither way agree with you and it's really "less-recommended" to modify the global query, and what's more no indication which pages you are affecting (and that affects any existing page with
cpt
publications
). Instead, it's better to make a custom query (instead of altering whole site) within the targeted .php
. However, you have right to downvote my question (with incorrect reason), and ok, it's your right.– T.Todua
9 mins ago
Creating a new WP_Query in this context leads to querying the database twice. As you have a second query over the main one.
– FFrewin
7 mins ago
Creating a new WP_Query in this context leads to querying the database twice. As you have a second query over the main one.
– FFrewin
7 mins ago
@FFrewin to say, actually i dont see any problem with that. You are not re-creating new queries on all pages, instead doing on sepcific pages, and even i dont think you have million of hits in hour on that page, to worry about DB overload (which is really good habbit to worry about overload and always try to have optimized code). But, in this case i dont see real problem. ok, i just suggested my answer, you decide.
– T.Todua
4 mins ago
@FFrewin to say, actually i dont see any problem with that. You are not re-creating new queries on all pages, instead doing on sepcific pages, and even i dont think you have million of hits in hour on that page, to worry about DB overload (which is really good habbit to worry about overload and always try to have optimized code). But, in this case i dont see real problem. ok, i just suggested my answer, you decide.
– T.Todua
4 mins ago
add a comment |Â
VeeJay is a new contributor. Be nice, and check out our Code of Conduct.
VeeJay is a new contributor. Be nice, and check out our Code of Conduct.
VeeJay is a new contributor. Be nice, and check out our Code of Conduct.
VeeJay is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fwordpress.stackexchange.com%2fquestions%2f315582%2fis-it-possible-to-remove-the-posts-per-page-limit-on-a-specific-post-type%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password