I believe the best way to do this in a plugin is to run the following sample function when the pre_get_posts
action hook is encountered. The $wp_query
object is available, meaning your conditional tags are available, but before WordPress gets the posts, which means you are changing query vars prior to the first query being run, rather than adding a second query like when query_posts() is used in a theme file.
function custom_posts_per_page($query) {
if (is_home()) {
$query->set('posts_per_page', 8);
}
if (is_search()) {
$query->set('posts_per_page', -1);
}
if (is_archive()) {
$query->set('posts_per_page', 25);
} //endif
} //function
//this adds the function above to the 'pre_get_posts' action
add_action('pre_get_posts', 'custom_posts_per_page');
Related Posts:
- How to Add jQuery Infinite Scroll to My Custom Archive Page
- [Plugin: Posts 2 Posts] How does it work?
- Post query – show posts from specified day and month and whole years
- What is the easiest way to create a custom field archive?
- Manually set global $post vars with an ID, in order to use template tags
- How to prevent a post from being deleted?
- How can I make it so the Add New Post page has Visibility set to Private by default?
- How to moderate posts
- Archive list with only years and months
- Limit access to posts/pages by user roles
- Show Similar Post Titles ( Similar to Stack Exchange )
- How to add option box in “Edit Post” plugin API?
- How Do I Use The WordPress Plugin Posts 2 Posts by Scribu?
- How do I unlock a post programmatically?
- How to work with Syntax Highlighter Evolved?
- User-Specific Timezones? Hack? Plugin?
- Is their any way to Extend WPDB class and Overwrite the Default Query Function
- Delete Post With No Traffic?
- Automatically Creating Posts for Popular Forum Topics or Products [closed]
- Let readers suggest edits from the frontend
- Using nextGen Gallery.. how can I call a list of all Gallery Names?
- Search and replace text across all posts
- Can we completely remove the WordPress Sitemaps (WordPress 5.5)?
- jQuery plugin only in one page/post
- Disallow a user to post in certain categories
- How To Ignore a Filter On Applying Filter the Content In a Function
- Auto-post to multiple sites like Posterous? [closed]
- Article source link for posts
- publish_post hook isn’t working for scheduled posts
- How to get posts by content?
- Share buttons on article footer
- How to create a Page alias in WordPress
- Plugin for changing a post’s category based on it’s post date?
- Change Permalinks Structure to a Sequential Number for Each Post?
- Publish Post After Click On A Link
- bulk post_content update
- WP get_permalink Return Wrong URL
- How to extract images of post and pages excluding header and logo image in wordpress?
- Dynamic HTML not displaying at respective place
- Fastest way to loop through all posts?
- Get the last post ID
- Enable comments for post with comments meta box removed
- Edit/revise option for authors to suggest improvement to posts [closed]
- Is there a plugin to display certain posts on certain pages? [closed]
- Custom Post Type causes Page Not Found
- Unique post-id for WordPress Multisite Network
- Is there an action that is called when a post is restored from the trash?
- plugin for wp_list_categories with posts
- How to filter content post only on save
- post expire after x days
- Action on post publish
- Replace existing pages with new pages, keep menu links
- Adding “ ” before the last word in multiple defined areas with a plugin
- Set attached to state
- Use plugin or custom post type for game score functionality
- Tag link suggestion plugin
- How to add new tabs in post.php
- How to change ID of an attachment in wp_posts and its related tables correctly?
- Collaborative post submission [closed]
- CKEditor: Uncaught TypeError: switchEditors.switchto is not a functiononclick
- New post notification plugin [closed]
- Is there plugin to show recent posts from one website in the widget area of another?
- Bulk Image Uploader to create new post from each image [closed]
- How use %like% in sql statement wordpress
- Display comments of users on single page
- Get the url of the full sized attachment image using post ID?
- Adding Plugin-specific Fields with wp_insert_post()?
- Adding Attachment Display Settings in custom media frame
- Output HTML only on individual post view
- Using the “Latest posts” feature on a different site
- Is there a way to order posts and custom post types as one group?
- Execute a plugin only on post pages
- How can i list random post from multiple category?
- Support for simultaneous editing
- How to get 1 or 2 specific posts on top of my wordpress blog?
- Portfolio + Blog: multisite or plugin?
- Accessing post’s meta data based on user’s click of a post
- Plugin echos text from shortcode function in gutenberg page editor
- View Private Published Page with URL Code (no login required)
- With W3 Total Cache when I publish a post it does not appear in the homepage. Only if i purge all cache [closed]
- The problem with WordPress Importer
- WordPress not updating post or disable plugin instead a white page appear
- Proper way to trigger a MySQL query via link in a plugin
- get post excerpt by query
- Ger posts from similar tags and categories
- Related posts popup
- get_var result empty for query of custom table in plugin admin
- Posts are deleted everyday at night
- How to Batch Upload .docx Files as Posts
- Category archive in menu
- Internationalization of Blog Posts
- Issues with Post 2 Post Plugin
- Find And Add Category to Posts
- Retrieve post thumbnails
- WP-Snap too slow (caused by WP_Query?)
- WordPress ultra slow if I click on posts?
- Enforce conditions only for draft posts using WyPiekacz, ignore pending and published posts
- How to add character to content of post?
- Get page type to display content
- Insert data in custom table during new post creation