posts_per_page sin límite
-
-
Heestadobuscandoen Googlepor untiempo ybuscandoen el códice WP,peronopuedoencontrar una respuesta directa aesta simplepregunta.Creo quemi preguntaestá redactada claramente con un código deejemplo y cuálfuemi intento (establecer un valor altoparael argumento).No soy unexpertoen WP,poreso vine aquípara hacer lapregunta.Inclusoproporcionar respuestas apreguntas que leparecentrivialeses útilpara hacer crecerestas comunidades de Stack Exchange.Personalmente,me encanta ver unenlace de Stack Overflowen mis resultados debúsqueda,a diferencia de unenlace a unforo demierda.I've been Googling for awhile and searching the WP codex, but I just can't find a direct answer to this simple question. I believe my question is worded clearly with example code, and what my attempt was (setting a high value for the argument). I'm not a WP expert, so thats why I came here to ask the question. Even providing answers to questions that seem trivial to you are helpful in growing these Stack Exchange communities. I personally love seeing a Stack Overflow link in my search results, as opposed to a link to a crappy forum.
- 6
- 2011-12-11
- Banjer
-
Además,graciaspor la respuesta.Debespublicarlo como una respuesta yno como un comentario,para quepueda aceptarlo.Also, thanks for the answer. You should post it as an answer and not a comment, so I can accept it.
- 0
- 2011-12-11
- Banjer
-
Entiendo supunto y aprecio suesfuerzoen *escribir * lapregunta.Tambiénestoy de acuerdoen que laspreguntas de losnoexpertospueden resultar valiosasen esta comunidad.Por otro lado,muchas deestaspreguntaspueden disuadir a algunosexpertos deinvolucrarse aquí.Setrata de unaespecie deequilibrio,supongo.Detodosmodos,soy ungran votante,así queespero suspróximaspreguntas :) Diviértase aquíen WPSE.I've got your point and I do appreciate your effort in *writing* the question. I also agree that non-expert questions can prove themselves valuable in this community. On the other hand too many of such questions can discourage some experts from getting involved here. It's all about some kind of balance I guess. Anyway I'm a big up-voter so I'm looking forward to your next questions :) Have a great time here on WPSE.
- 0
- 2011-12-11
- Michal Mau
-
PD:también desea reemplazar `type`por`post_type` (oeliminaresta líneapor completo).Editarétanto la respuesta de Rutwick como supreguntaparaevitar que alguien copie ypegueestepequeñoerror.PS: you also want to replace `type` for `post_type` (or remove this line altogether). I will edit both the Rutwick's answer and your question to prevent somebody from copy-pasting this little mistake.
- 0
- 2011-12-11
- Michal Mau
-
@Maugly Graciasporel hombre de corrección ... ¡solo se centróen posts_per_page,por lotanto,copióelerrortipográfico!;)@Maugly Thanks for the correction man... just focused on posts_per_page hence copied the typo! ;)
- 0
- 2011-12-11
- Rutwick Gangurde
-
6 respuestas
- votos
-
- 2011-12-11
¡-1estu respuesta!Busque
posts_per_page
aquí .$args = array( 'post_type' => 'post', 'cat' => '22,47,67', 'orderby' => 'name', 'order' => 'ASC', 'hide_empty' => 1, 'depth' => 1, 'posts_per_page' => -1 );
Advertenciaimportante :estopuede resultaren una consultamuygrande quepuede hacer queel sitio caiga.Hagaesto solo siestá seguro de que subase de datospuedemanejarlo.Noen temaspúblicos o complementos.
-1 is your answer! Look for
posts_per_page
here.$args = array( 'post_type' => 'post', 'cat' => '22,47,67', 'orderby' => 'name', 'order' => 'ASC', 'hide_empty' => 1, 'depth' => 1, 'posts_per_page' => -1 );
Important caveat: This can result in a very huge query that can bring the site down. Do this only if you are sure your database can handle it. Not in public themes or plugins.
-
Advertenciaimportante:estopuede resultaren una consultamuygrande quepuede hacer queel sitio caiga.Hagaesto solo siestá ** seguro ** de que subase de datospuedemanejarlo.Noen temaspúblicos o complementos.Important caveat: This can result in a very huge query that can bring the site down. Do this only if you are **sure** your database can handle it. Not in public themes or plugins.
- 7
- 2016-08-11
- fuxia
-
@toscho Agregando su comentario como una actualización de la respuesta.@toscho Adding your comment as an update to the answer.
- 0
- 2016-11-23
- Rutwick Gangurde
-
¡¡salvasmi vida!!you save my life!!
- 0
- 2019-10-11
- Darlan Dieterich
-
@DarlanDieterich ¡Me alegro de haberpodido ayudar!:)@DarlanDieterich Glad I could help! :)
- 0
- 2019-10-14
- Rutwick Gangurde
-
- 2011-12-12
O,alternativamente,puedepasar
WP_Query
(quees lo quequery_posts
usa)el argumentonopaging
,quebásicamente hace lomismo ..$args = array( 'nopaging' => true // Your other args, etc.. );
Haráexactamente lomismo,pero sitiene que volver amirarlomástarde ynopuede recordar lo queestaba haciendo,personalmente creo que le quedarámás claro quées lo quepretendía coneseparámetro.dentro de lamatriz de argumentos.
Sinembargo,comomencioné,ambos lograrán lomismo.
Noestá demástenermás de unenfoque,y siempreesbueno compartir lo que sabes,basta con decir queesaes la razón demi respuesta,apesar de que yatienes suficiente ...;)
Or alternatively you can pass
WP_Query
(which is whatquery_posts
uses) thenopaging
argument, which basically does the same thing..$args = array( 'nopaging' => true // Your other args, etc.. );
It will do exactly the same, but if you have to look back at it later and can't remember what you were doing, i personally feel it will be more clear to you, what it is you were intending with that parameter inside the args array.
As i mentioned however, they'll both actually achieve the same.
Can't hurt to have more than one approach, and it's always nice to share what you know, suffice to say that's the reason for my answer, despite you already having a sufficient one.. ;)
-
- 2014-02-23
Desdeel archivo defunciones detemas de su hijo:
add_action( 'pre_get_posts', 'wpsites_no_limit_posts' ); function wpsites_no_limit_posts( $query ) { if( $query->is_main_query() && !is_admin() && is_home() ) { $query->set( 'posts_per_page', '-1' ); $query->set( 'order', 'ASC' ); $query->set( 'post_type', 'post' ); $query->set( 'cat', '22,47,67' ); $query->set( 'orderby', 'name' ); $query->set( 'order', 'ASC' ); $query->set( 'hide_empty', '1' ); $query->set( 'depth', '1' ); } }
From your child themes functions file:
add_action( 'pre_get_posts', 'wpsites_no_limit_posts' ); function wpsites_no_limit_posts( $query ) { if( $query->is_main_query() && !is_admin() && is_home() ) { $query->set( 'posts_per_page', '-1' ); $query->set( 'order', 'ASC' ); $query->set( 'post_type', 'post' ); $query->set( 'cat', '22,47,67' ); $query->set( 'orderby', 'name' ); $query->set( 'order', 'ASC' ); $query->set( 'hide_empty', '1' ); $query->set( 'depth', '1' ); } }
-
- 2019-10-17
Usando Ricardo con algunasmodificaciones:
static $paged; $post_ids = []; do { $paged++; $defaults = [ 'fields' => 'ids', // Only return the ID field to make this query faster. 'post_type' => 'post', 'posts_per_page' => 100, 'no_found_rows' => false, // We need pagination & the count for all posts found. 'paged' => $paged, 'update_post_term_cache' => false, 'update_post_meta_cache' => false, ]; $query = new WP_Query($defaults); if ($query->have_posts()) { foreach ($query->posts as $id) { $post_ids[] = $id; } } } while ($query->max_num_pages > $paged); return $post_ids;
Esto aumentará drásticamenteeltiempo de consulta al consultar solo lafila de ID yevitar la actualización detérminos ymeta caché.
Using Ricardo's with some modification:
static $paged; $post_ids = []; do { $paged++; $defaults = [ 'fields' => 'ids', // Only return the ID field to make this query faster. 'post_type' => 'post', 'posts_per_page' => 100, 'no_found_rows' => false, // We need pagination & the count for all posts found. 'paged' => $paged, 'update_post_term_cache' => false, 'update_post_meta_cache' => false, ]; $query = new WP_Query($defaults); if ($query->have_posts()) { foreach ($query->posts as $id) { $post_ids[] = $id; } } } while ($query->max_num_pages > $paged); return $post_ids;
This will dramatically increase the query time by only querying against the ID row and avoiding updating term and meta cache.
-
¡Agradable!graciaspor compartir.Nice! thanks for sharing.
- 0
- 2019-10-18
- Ricardo Canelas
-
- 2016-08-11
La respuesta correctapara suproblemaes
'posts_per_page' => -1
porque-1
devolverápublicacionesilimitadasporpágina como responden los demás usuarios.Solo quiero agregar un complemento aesta Q/A,
Si desea obtenerelnúmero depublicacionesporpágina de la configuración de lecturaen el Panel de administración de WordPress,debe llamar a lafunción
get_option()
ypasarposts_per_page
comocadena a él.$args = array( 'post_type' => 'post', 'cat' => '22,47,67', 'orderby' => 'name', 'order' => 'ASC', 'hide_empty' => 1, 'depth' => 1, 'posts_per_page' => get_option('posts_per_page') );
Espero queesta respuesta ayude a alguien como amí.Usuariosfelices de Stackexchange de codificación
The right answer for your issue is
'posts_per_page' => -1
because-1
will return unlimited posts per page As the others users answer.I just want to add an add-on to this Q/A,
If you want to get the number of posts per page from the reading setting on WordPress Administration Panel you have to call the
get_option()
function and passposts_per_page
as a string to it.$args = array( 'post_type' => 'post', 'cat' => '22,47,67', 'orderby' => 'name', 'order' => 'ASC', 'hide_empty' => 1, 'depth' => 1, 'posts_per_page' => get_option('posts_per_page') );
I hope this answer will help someone as it help me. Happy Coding Stackexchange Users
-
¡Esoes realmente unabuena adición!That's actually a nice addition!
- 0
- 2017-12-29
- Herbert Van-Vliet
-
- 2017-02-22
O ...
function getAll($arg, $posts_per_page = 50) { $data = array(); $page = 1; do{ $arg['paged'] = $page; $arg['posts_per_page'] = $posts_per_page; $query = new WP_Query($arg); foreach ($query->posts as $post){ $data[] = $post; } $page++; }while(count($query->posts) === $posts_per_page); return $data; }
Or..
function getAll($arg, $posts_per_page = 50) { $data = array(); $page = 1; do{ $arg['paged'] = $page; $arg['posts_per_page'] = $posts_per_page; $query = new WP_Query($arg); foreach ($query->posts as $post){ $data[] = $post; } $page++; }while(count($query->posts) === $posts_per_page); return $data; }
-
Debe agregar contextoparaexplicar su código,suideapara resolver lapregunta.You should add context to explain your code, your idea to solve the question.
- 2
- 2017-02-22
- bueltge
Quiero devolver TODAS laspublicaciones con
query_posts
.Intenté configurarposts_per_page
en unnúmero realmente alto,peroquery_posts
se asusta yno devuelveningunapublicación.¿Cuáles laforma correcta de consultarpublicaciones sin límite?