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 query posts based on lat-lng coordinate as post meta?
- How to trigger 404 for custom query var?
- How do I create my own nested meta_query using posts_where / posts_join?
- Best Practice For Querying Grandchildren?
- wp_query display posts from same category of the post
- How to prevent queried posts from being added to cache?
- Order posts by meta value and Date
- Include Sticky Posts in Page Post Count for custom query
- How to implement time filter to show random post 1 month for one category and 3 months for other categories
- Can you order posts from wp_query by two custom meta fields?
- Pagination working everywhere but the index page
- Conditional arguments WP_Query for post custom fields
- How do I search inside specific taxonomies in WordPress
- What’s the purpose / logic of split_the_query in WP_Query->get_posts
- How to loop for every result found in the_content() when using the search query?
- if statement in wp_query arguments
- How to build a WP_Query using mulitple tags and using AND or OR operator between them
- new WP_Query to get max price meta value not working
- Pagination links missing for first link (1) and previous button? How to get pagination links to work?
- wordpress search word, “hello world” ===> ‘hello+world’ ===> ‘hello’, ‘world’
- WP_Query with different postmeta filter for each categories
- Automatically Query Parent Taxonomy
- wordpress ajax relationship query
- Shouldn’t I be able to modify the main query by this filter?
- Get all posts which was posted on X Days WordPress
- Admin Page Post Id wp_reset_postdata not working
- Display single most recent post in category on every page of category archive
- wp_query comment and meta query
- Show menus to one admin username
- inserting content of 1 Post to in another with a template hierarchy
- Get the child category ID of current category
- Limit number of posts in WP_query per month
- Use have_posts() with array of post results retrieved by $wpdb->get_results
- how to translate countries output from wp database?
- Custom query does not find pages
- Display posts ONLY from the current logged in user and current week/year/month Elementor posts
- What’s wrong with this meta query? (order by meta key, then title, doesn’t work)
- Change normal grids to masonry grids
- XMLRPC pingback.extensions.getPingbacks not work with parameters
- Get posts meta_query by repater field
- WordPress pagination not returning posts on second page
- How can I use two custom fields in WP Query
- How can I use a specific wordpress page template if certain words are used in page title [closed]
- wp-query with a tax-query relation of NOT
- Custom query using WP_Query is not working
- Optimising amount of calls to custom fields
- Issue in If else condition [closed]
- Extract video content from post to display on front page
- Carousel slider with WP_Query to show 3 posts on each slide
- How to replecate the _fields parameter in custom REST api endpoint
- Sort by multiple columns using get_posts
- WP_Query filter Posts by timestamp event (range start and end) and by month (next 12 month)