showposts
is an alias of posts_per_page
, which is a private query var, meaning it can’t be set from the query string. You could register your own query var and map that to posts_per_page
in a pre_get_posts
action. This could go in your theme’s functions.php
and would work for any main query.
// add a new query var to the list of known query vars
function wpd_custom_posts_queryvar( $vars ){
$vars[] = 'wpd-num';
return $vars;
}
add_filter( 'query_vars', 'wpd_custom_posts_queryvar' );
// hook pre_get_posts and set posts_per_page
// if it's the main query and our custom query var is set
function wpd_custom_posts_number( $query ){
if( $query->is_main_query() && $query->get( 'wpd-num' ) ){
$query->set( 'posts_per_page', $query->get( 'wpd-num' ) );
}
}
add_action( 'pre_get_posts', 'wpd_custom_posts_number' );
Related Posts:
- How to add category to: ‘wp-admin/post-new.php’?
- Exclude posts that only have the ‘Uncategorized’ category [duplicate]
- Show related posts by category but ignore one category
- prevent showing posts of an specific category in admin posts section
- Exclude posts from featuring
- How can I remove posts of a certain category from homepage after a specified time period?
- Random post category URL
- List posts in a given category
- Assign category to new post via URL
- Get user categories with most posts in it
- Redirect to another page using contact form 7? [closed]
- Loop doesn’t exclude the specified category in home page
- Display posts of specific category term
- Migrate posts from category and sub-category via SQL
- WP_Query of Category Not Showing First Post
- Sidebar limiting to 10 posts?
- Can I show category name in url for only one of my categories?
- Query prints posts without specific categories
- Does having category name in permalinks affect SEO when having a post in multiple categories?
- How to add a ‘News’ section to specific posts in WordPress
- show image gallery in archives or category page
- Page with Category Returning 1
- Get 5 most recent categories
- Querying posts from current category, using a variable as array argument
- Order posts by separate menu order for different sub-categories
- Retrieve latest post by multiple categories with ID
- Make assigning post to a specific category equivalent to assigning it to all categories
- How to Include a common category in a custom Category Search
- WP_Query: Fetch posts that are in (category1 and not in category2), OR posts that are not in cagegory1
- Exclude sub sub categories from displaying in the archive page
- List posts related to category on a div [closed]
- Show Title/Date/Excerpt of first post & only Title for rest in Query [duplicate]
- Filters do not work when there are multiple (one works)
- Using permalinks, category slugs, and tag slugs
- post categories
- Search by post title and content in wp_query without order
- Hiding Draft Post In Admin
- Exclude category from
- How to limit posts by category
- How do I correctly set up a WP-Query to only show upcoming event-posts?
- Blog post per page setting conflicting with custom WP_Query?
- Change post order random through out the entire WordPress
- How to insert 2 args into 1 Wp_Query for a slideshow
- How to exclude latest x posts from a paginated query?
- Setup a custom dynamic post
- Stop users posting into category while still seeing contents of the category
- Anyway to grab author name and featured image in an embeded in inital array of posts outside of loop?
- How to verify wp user password by sql query in wp? [closed]
- Removing noindex posts from wp_query
- Category Page Template not Showing Posts
- display a random post thumbnail from a random category
- How do I retrieve then print the currently viewed sub categories?
- WP Query – Posts Per Page not working in combination with category__in
- Ajaxify This Code
- How to allow users to post only in certain category and hide elements from edit page?
- How to filter my search in post if contains a word in title, content or excerpt?
- How to return another post than requested
- filter RSS feed in URL
- How can i limit the number of posts created per category?
- Pausing and Resuming WP_Query results
- How to do set post permalinks using 6 digit random unique function?
- How to Restrict Previous & Next Post Link to Posts of Same Category?
- Select only post id and meta value with WP_Query
- Pulling in post category and children within category
- WordPress custom Query for Posts in Category display posts multiple times
- Lost draft under all posts and drafts
- Post categories to pages
- Post page to display specific category
- Get Posts Under Custom Taxonomy
- Get attachments for posts that belongs to a specific category
- Prevent additional top level categories but allow more children
- Need help with category listing!
- Random ajax load only works with posts_per_page set to -1
- Prepending %category% onto default posts fails
- List all categories with featured image from post?
- How to : pagination in 3 different custom loops on the same page
- Categories list into registration form
- WP_Query: Mixing category__in and tag__in together
- How safe is renovating $wp_query when doing WP_Query pagination in Page Template
- If in_category not working for multiple single.php pages
- How can I setup a relationship using categories in WordPress?
- WordPress Multisite – Create Default Post and New Category On New Site Install
- Exclude the first ‘n’ number of posts of a tag from home page?
- how to show single post in a custom template
- Query posts and return XML
- Using ajax on editing a category edit page?
- how to change the number of posts returned in a specific loop without making it global?
- Related posts by category not working right
- How to list recent posts in a wp nav menu?
- Set Default Category to Username
- Query Posts With Over 1000 Views
- Disable sticky option for specific categories
- Dynamic dependent Dropdown lists for categories, sub-categories and posts
- How can I hide tags on a child-category page, if that tag has not been used?
- date issue with category post retrival
- posts_per_page option limits the number of Gallery items
- List authors with the last post title and order by last post date
- How to order posts by meta_value and title
- Listing Specific Categories from Current Post with Depth
- How to make multiple sections in home pulling posts category wise?