I'm working on a site where the client wants only the linked titles of all posts in a particular category on one page. That is, the query does not pick up the smaller number of posts that is defined in Settings from that one particular template.
I could do this if I by-pass the loop entirely, but is it possible within the normal loop?
I'm working on a site where the client wants only the linked titles of all posts in a particular category on one page. That is, the query does not pick up the smaller number of posts that is defined in Settings from that one particular template.
I could do this if I by-pass the loop entirely, but is it possible within the normal loop?
Share Improve this question asked May 17, 2020 at 21:15 Nora McDougall-CollinsNora McDougall-Collins 3952 silver badges15 bronze badges1 Answer
Reset to default 2The best way to do this is to use the pre_get_posts
filter, which changes the main query. That way you're not running the default query plus a custom one on top of it. You'll have to determine the "if" conditions to only run on the query you want to affect:
<?php
function wpse_366882_get_all_posts($query) {
// This will run for all main queries that are not in wp-admin.
// You may want "is_archive()", "is_page('slug')" or some other condition.
if ( ! is_admin() && $query->is_main_query() ) {
// Setting to -1 shows all.
$query->set( 'posts_per_page', -1 );
}
}
add_action( 'pre_get_posts', 'wpse_366882_get_all_posts' );
?>
You can place this code in a custom plugin, or in a custom theme or child theme's functions.php
file.