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
- How Do I Use The WordPress Plugin Posts 2 Posts by Scribu?
- publish_post hook isn’t working for scheduled posts
- Plugin for changing a post’s category based on it’s post date?
- Publish Post After Click On A Link
- bulk post_content update
- Enable comments for post with comments meta box removed
- Edit/revise option for authors to suggest improvement to posts [closed]
- Is there an action that is called when a post is restored from the trash?
- plugin for wp_list_categories with posts
- 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
- Use plugin or custom post type for game score functionality
- How to add new tabs in post.php
- How to change ID of an attachment in wp_posts and its related tables correctly?
- New post notification plugin [closed]
- Bulk Image Uploader to create new post from each image [closed]
- Adding Plugin-specific Fields with wp_insert_post()?
- Using the “Latest posts” feature on a different site
- Is there a way to order posts and custom post types as one group?
- How can i list random post from multiple category?
- Plugin echos text from shortcode function in gutenberg page editor
- Cron job not firing
- Is there an easy way to flag posts in the admin area?
- finding whether request is for post, and post id
- Can’t delete Draft Post [closed]
- Fetch Children of Grouped Products Inside WooCommerce Product Loop
- Outbound link autometic converted into linkdirect , and not opening
- How to allow an editor to edit all WordPress blog posts but have the last say as administrator?
- Contributive page where people logged in can write
- Trying to fix form resubmition with PRG (Getting error: Cannot modify header information – headers already sent by)
- WP Query. Is there a maximum size?
- Display wordpress post’s in popup?
- Unable to add TAB character to post?
- AJAX fileupload – TypeError: not a function ajaxSubmit()
- How to get the first post and second post in a wordpress blog?
- Recent posts per category loop?
- ‘Organize Series Plugin’ as muti author feature
- Yoast SEO settings unavailable in an archive page from wordpress plugin
- correct sql query
- SQL query, error
- Problem of encoding characters (apostrophes) in my posts publications
- How to backup single post in wordpress [closed]
- Is there a way to make the main page only display a brief description of the full article?
- How to change the post form from plugin?
- Allowing users to Sign-up > Login > Post articles that need approval
- posts from multiple post types in one slider
- How to display the featured post on the category page?
- Set Multiple Meta Values as an Array Using dispatch( ‘core/editor’ ).editPost() Call in Gutenberg/JS
- Is there a plugin or a way in the wordpress that would let us have different versions of a post or page accessibe to users?
- download button for audio file in post [closed]
- Print posts category or tag as a formatted journal
- How to return the values from a row where a value occurs for the first time among the rows available?
- Post deleted in trash [closed]
- How to relate 3 wordpress components with each other (Create database relationships)
- WordPress plugin: Getting posts where checkbox selected
- Archive with months of current year only
- problem with register_activation_hook in plugin
- How to auto strip hyperlinks & images in wordpress post
- WP Editor, save content and category on frontend
- Plugin for TinyMCE to create reference to other posts
- How to get Recent Post From Each Category with Thumbnail?
- help intercepting save_post through plugin
- What snippet do I need to type to show my ACF field show up on my theme?
- Is there a way to make [Table Of Content] plugin while not using revision data?
- Why the internal link get converted into a comment?
- Is it possible to pin a post in second position from top
- page duplication on wordpress that wont stop
- Copy/paste local images not working in WordPress
- How to change my 3000 Published post status to Draft using PHPMyAdmin
- the_posts_pagination() not working if I add wp_head() to header.php
- How can I see $post object in frontend from functions.php?
- .com creates automated link in post
- Allow members to create groups on my site
- All Post WordPress Page Error
- Is it possible to create duplicate post on other site (either push, on publish, or pull, periodically)?
- Add dynamic color to each category item
- Is there any hook to change media html in post? [duplicate]
- How to force regenerate thumbnails in WordPress posts?
- Publish Post but don’t call publish hook in foreground post creation
- Serialize $_POST global in wordpress
- SWF in wordpress post
- How to use shortcode of any plugin to show it visually where i want?
- WordPress not updating post or disable plugin instead a white page appear
- get post excerpt by query
- Internationalization of Blog Posts
- Issues with Post 2 Post Plugin
- How to add character to content of post?
- why the current date do not update
- What is the correct method for updating post content from a plugin?
- Custom feed parameters / Template overriding
- Next and Previous Pagination button not displaying in WordPress
- Plugin to display text before a post
- Sell “create a post”, paid account for post creation
- Shortcode returning specific content of a post
- How to get an error message if a form is empty (plugin: Post for site) [closed]
- Is there a plugin or another way that can help me find out the underlying files of a page/post? [closed]