You need to wrap your code in a loop. Instead of if ( $query->have_posts() ) {
, use:
while ( $query->have_posts() ) {
$query->the_post();
// Rest of code
}
wp_reset_postdata();
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 take parameters from a function and make them editable as attributes in a 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?
- 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 for a week using key => value WP_Query argument notation?
- How to use filter hook posts_join for querying taxonomy terms in posts_where?
- WordPress altering my custom query, How to fix it?
- Looping through tabular data
- How to create/modfiy WP_Query to search in post title OR custom field?
- How to detect custom query inside `posts_where` hook?
- Modifying the Search Results
- Offset Page Loops and Pagination
- How to find a post id using the post_excerpt?
- reducing the amount of wp_query calls
- Use post object from first query in second query
- Order by title without taking into account ‘the’
- Set global $wp_query/$post variable for dynamic page generation
- meta_value_num not ordering all items
- Meta Queries – should nesting work after WP 4.1?
- Is there a better way to pull in custom content without querying posts?
- WordPress | Date not always appear [duplicate]
- Pass $page_id to new WP_Query
- Include posts from some categories while excluding from others
- Multiple Rows in a using wp_query
- “paged” in WP_Query returns 0 posts
- Change AJAX filter from POST to GET for URL Parameters
- How to create a widget like this?
- How can I update post meta inside a WP_Query loop or the WordPress loop?
- Printing a variable inside a custom WP_Query
- Creating attachments archive in tags and categories
- Does query_posts have an effect on get_the_category?
- Second loop pagination changes URL, not content
- Help with Wp Query
- tax_query in pre_get_posts
- How to prevent WP_Query from filtering on ‘s’ but keep ‘s’ for other purposes?
- Query on meta values and post title
- Add dynamic search to paginated WP_Query
- Adding GROUP BY in Custom Query not working with ORDER BY
- Where is the coding error for the unexpected ‘}’ in this if / else choice of loops?
- Show custom content when loop or wp query reaches specific time?
- Query Order By Number Variable From API
- Wp includes access control
- Sorting query_posts() with a complex orderby filter
- Order posts by title and custom field value?
- How to exclude posts by ID within a category/archive loop
- Dynamically Update Page Content
- Optimal way to make tags in tax_query optional?
- Exclude all tagged posts from WP_Query loop
- Bring a post to the top of the query if it’s in a certain category?
- WP_query for posts with same post type AND same post category
- Custom query with ‘tax_query’ and ‘meta_key’ returning no results
- Get the type of an advanced custom field (ACF) in a WP_Query loop [closed]
- loop through custom post types with meta data
- Looking for a way to exclude frontpage and nav menu from query filter
- Using Query In Post Type Archives
- WP Query Obj: Set value to be unequal | Hide media by admin