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 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?
- Combining two wordpress queries with pagination is not working
- Executing Queries in tag.php
- Get list of posts which have at least one term from a custom taxonomy with WP_Query
- WP_Query using meta_query with relation OR and orderby meta_value doesn’t work
- WP_Query meta_query by array key
- Specific loop in Shortcode
- Trying to list out child pages with WP_Query
- view queries made?
- How to show only one post for each categories of taxonomy of custom post that contains a specific custom field
- 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
- WP_Tax_Query with post_tag not working
- Count total number of post in foreach loop
- WP_Query not retrieving by random order under parent
- Get images only if parent status is publish
- How to show recent and upcoming birthdays using meta_query
- How to change posts order on Category and Tag archives?
- How can I retrieve comments from last 5 minutes for a post?
- 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
- WordPress: Display posts by custom select query
- query offset countdown by 2 each day?
- Get ONLY the number of results in a query
- Include custom field in Ajax search WordPress
- Inserting serialized value into wp_postmeta using update_post_meta
- Display custom field meta outside loop, site wide
- Where is the WP_Query instance instantiated (when working with themes)?
- Iterating through $wpdb query without using get_results for large query results
- 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
- Conflict with get_posts and the_content
- Woocommerce set loop_shop_columns to be 3 in the main shop page loop only and 4 otherwise
- Get posts using multiple values from ACF checkbox as meta query wordpress
- How to save the results of a query as a php file for an autocomplete search bar
- Why ignore_sticky_posts argument is in sticky post query?
- Complicated `orderby` based on text in custom field
- meta_query post_date not returning results
- WP Query: orderby with one meta key, but multiple values
- WP_Query returns wrong post when searching by title?
- WP Query to display events and posts at the same page
- get posts returns results but wp_query does not