Essentially you are asking how to pass parameters to a shortcode. This can be done with the function shortcode_atts, which has two required parameters: an array of valid parameters and their defaults, and the attributes of the shortcode that is being passed. You would use it like this:
add_shortcode( 'wpse232385_show_news', 'wpse232385_news_query' );
function wpse232385_news_query ($atts) {
$news_atts = shortcode_atts(
array(
'posts_per_page' => 3,
'category_name' => 'news' ),
$atts );
$args = array(
'posts_per_page' => $news_atts['posts_per_page'],
'category_name' => $news_atts['category_name'],
);
$news_query = new WP_Query( $args );
...
}
Related Posts:
- How to paginate wordpress [gallery] shortcode?
- What kind of object type is WP_Query?
- Last post ordered by publish date using WP_Query on front page
- Shortcode returns escaped HTML tags
- WP_Query in a shortcode
- ‘&’ causes an error in my shortcode when I list the content of the page
- Can certain (site-crashing) limitations on WP_Query in shortcode be overcome?
- How to retrieve current page WP_Query arguments?
- wp_query add arguments using array_push if variable met
- Category attribute not working in custom shortcode
- How to get all child pages in their parent page?
- Database Queries Optimization with new WP_Query
- Orderby is working with one query but not with other
- Shortcode for latest -not expired- posts
- Gallery shortcode numerical sorting
- How to filter a query by date in a shortcode?
- How should I approach changing the template & $query as part of a shortcode’s execution?
- Shortcode Not Working in Slider When Added To Post Title
- Shortcode for custom query not returning results when attributes are added
- How to Display Shortcode in the correct location, currently displaying before content after Page Title?
- WP_query only displays one of my custom post type entries
- Assign a minimum result count for WooCommerce query shortcodes?
- Using a Shortcode to List Posts from Multiple Categories
- embedding shortcodes in php template
- How to get specific string in explode array?
- Trouble passing attribute into shortcode function
- Woocommerce Shortcode which displays a table with product data
- WP Query by 4 different taxonomies
- create custom shortcode wp and put php code in
- How to display a post(by id) along with css in a page?
- Shortcode attributes causes 500 error after updating the page
- pages shortcode filtering by category
- WP_Query & shortcode : Return 3 articles from a category WordPress
- Query by Category and Custom Field – Shortcode
- How to modify WP Query to target the first most recent post to adjust content
- Object of class WP_Query could not be converted to int inside shortcode
- How can I create another instance of my custom shortcode
- Get Current CPT taxonomy category in shortcode
- Custom shortcode not displaying categories and correct date
- return wp query results in a shortcode
- More efficient query to display posts in same sub-category?
- Having Trouble Running Query From Shortcode Using Tribe’s Events Plugin
- shortcode using multiple WP_Query’s with multiple category names not fully functional
- Shortcode does not display pagination [duplicate]
- How to write a query-function as a query-shortcode?
- shortcode with $atts with strange results
- WP_query shortcode inside acf Repeater breaks the repeater loop
- How to prevent WP query searching in executed shortcodes
- How to create a three column blog layout with a single query and with three different classes?
- How to force a query conditional?
- How to get order of posts?
- Executing Queries in tag.php
- Trying to list out child pages with WP_Query
- Intentionally exceed max_num_pages on main query without getting 404?
- Pass the same object to multiple widgets in a template with one query
- Get rewrite result
- Get images only if parent status is publish
- How to change posts order on Category and Tag archives?
- Using WP_Query and WP_Meta_Query Outside of WordPress
- Create a sitemap without a plugin – get parent pages only?
- WP_Query on custom field and order results
- Get ONLY the number of results in a query
- Filter posts in category archive page by year using a dropdown
- Show post number in widget loop
- Extend product search with meta in WooCommerce
- Custom Query by Author field and meta
- Custom wp_query for related posts with cmb2
- Include data from custom table in WP_Query
- Query posts by last comments
- WordPress ignoring LIMIT / posts_per_page despite being in wp_query
- Query Page Content From Theme Options?
- Taxonomy order exception for specific term
- next_post_link / previous_post_link not working with WP_Query
- How to Create a Random List of Child Pages
- How to make posts unqueryable/unpublish posts where ACF relationship field is an unpublished post? [closed]
- Get all posts with empty meta_value
- Count identical post titles
- get_page meta query not working, maybe a bug?
- Query by date from custom field
- What is wrong with my WP_Query Arguments?
- Query of all pages, including children, ordered by meta key
- specific post is not excluded from the loop
- How to WP Query custom multiple custom taxonomies?
- get_the_terms has strange result since version 6.0
- How to display posts from custom post type category(custom Taxonomy) wise?
- How to allow a variable with “/” in wordpress url
- How to get taxonomy image attached to a Post Type WordPress
- update_post_meta performance in a loop woocommerce
- Display posts side by side with custom query
- Is there a way to control both Order By and Order query parameters from one input field
- Custom Order Current Query: By Meta Key and Category
- Custom taxonomy and query multi conditions
- How to get query results for the next page
- Multiple loop for “featured” items returns wrong posts
- Query string form $_GET[‘value’] is not working as meta value in wp_query
- query_posts problem – need help
- 294 Queries on Mainpage of WordPress
- Pagination on custom post type not working
- WP Query – Show custom posts only if user contain some user meta
- How do I subquery with custom meta fields?