If you’re altering the main query, always use pre_get_posts
to alter query parameters before the query is run and before the template is loaded. This will be the most efficient and will not break pagination. As of v4, orderby
accepts an array of arguments, which gives you the ability to have different order
for each if necessary:
function my_get_posts( $query ){
if( !is_admin() && $query->is_category() && $query->is_main_query() ){
$query->set( 'posts_per_page', -1 ); // show all posts
$query->set( 'orderby', array('menu_order' => 'ASC', 'title' => 'ASC') );
}
}
add_action( 'pre_get_posts', 'my_get_posts' );
Related Posts:
- Is `query_posts` really slower than secondary query?
- Are there any scenarios where the query_posts may be used?
- Why is it necessary to call rewind_posts() when using the loop more than once? [duplicate]
- Multiple posts with one loop iteration
- Post Loop not Returning Permalink
- How to Loop within a Loop (Display Children and then Grandchildren)
- Exclude first 2 posts with meta_key from loop
- Better way to display posts from specific categories, in a grid layout
- popular post weekly and monthly
- 3 Posts in Loop, Show Stickies First
- Query last updated posts (posts updated in the last 24 hours)
- Displaying terms based on loop posts?
- Duplicated posts on category page
- Make blog page show 10 on first page, 9 on every other page
- How to display posts by vote count and if no value continue with latest posts with no votes?
- query_posts() vs get_posts() multiple loops [duplicate]
- Exclude first post (sticky or not) from the loop using query_posts()
- Pagination not working in category listing [duplicate]
- How can I improve the performance of this query_posts loop?
- Adding div after every two post on main loop! Why will first post not get counted?
- How can I access specific posts brought back by query_posts?
- Separate First Post Loop
- Query posts only with actual text content (not including shortcode or images)
- query_posts clarification needed
- Exclude current post ID from loop in sidepbar.php
- Why does my WP_Query pagination on a custom page.php only loads the homepage?
- Get a Post Loop based on Logged in User information into a Shortcode
- how to add 4 post in one carousel slide
- Query counting excluded category on paged loop
- get_posts and wp_autop (remove filter)
- query_posts ignores the argument
- The Loop isn’t working
- Twitter bootstrap carousel multiple items in carousel
- show posts from one category with comments only
- Least Number of Loops to Create Custom Homepage?
- How to do query_posts on tags pages
- Limitless amount of posts in custom archive page
- How to fix pagination for custom loops?
- Jquery Slider for profile template
- Counting the posts of a custom WordPress loop (WP_Query)?
- if ( is_home() && ! is_front_page() )
- Get excerpt using get_the_excerpt outside a loop
- How to display Yoast SEO meta description in archive template for each post instead of the_excerpt()? [closed]
- How do I exclude a custom taxonomy from the post loop
- Get post content from outside the loop
- Why should I put if(have_posts()), is while(have_posts()) not enough?
- Display featured products through custom loop in woocommerce on template page
- Split Content and Gallery
- Some doubts about how the main query and the custom query works in this custom theme?
- How to get Author ID outside the loop
- How to get posts published between a date and today?
- How can i display the content in plaintext
- Redirect loop when trying to login to /wp-admin/ [duplicate]
- How to split a loop into multiple columns
- Is it necessary to reset the query after using get_posts()?
- Is there any difference between the_title() and echo get_the_title()?
- AJAX with loop filtering categories
- WP_Query vs get_posts
- Do I need to use The Loop on pages?
- Remove the Homepage Query
- remove tags from the_content
- the_title() shows title of the first post instead of the page title?
- How to force excerpts / teasers in the loop
- Retrieve each widget separately from a sidebar
- Why am I being limited to ten posts on a custom loop?
- Should I use loop in the single.php file?
- A search for ‘0’ returns results
- How to return results of a get_posts() in explicitly defined order
- Why do themes rely on “The Loop”?
- Display all posts from specific categories on a page
- How to return loop contents
- Using the Loop to show all levels of subpages under a parent page? Halfway there
- Cleanest Way to Select Every Second Element in a Loop?
- Order posts by ID in the given order
- Insert image or ad script after 3 posts using the loop
- Get date of last update outside of loop
- Pagination not working on home page
- Child Pages Loop
- How to place comments_template(); outside the loop?
- Multiple Loops Homepage?
- Modify main WordPress loop with a parse_query filter
- How to Change Loop to Order Posts by Views (using wp-postviews plugin)
- Get the number of posts from the current page results
- how to upload image using wp_handle_upload
- Avoiding using a loop to access a single post
- How to exclude latest x posts from a paginated query?
- How to get the first image gallery of a product in woocommerce in a loop
- Get post by page name or slug
- WordPress Custom Query to show posts from last x years
- save_post + insert_post = infinite loop
- Build a content and excerpt grid loop with paging and options for # of posts
- Loop.php vs looping inside template file
- Get ID of a page containing secondary loop in content
- Custom Loop and Infinite Scroll
- Query Custom Meta Value with Increment
- Loop that displays PARENT PAGE & CHILD PAGE & outputs GRANDCHILD PAGE title and content
- is_home, and is_front_page conditional problem
- Display content from a specific category
- the_content and wp_link_pages
- Adding ‘current_post_item’ class to current post in the loop