The WP_Query
class does not generate HTML markup. WP_Query
will query the WordPress content, and provide some data back. So using WP_Query
and the sprintf()
PHP function, the HTML markup can be generated (untested):
$args = array(
...
);
$query = new WP_Query( $args );
$html="";
while ( $query->have_posts() ) {
$query->the_post();
$saved_html .= sprintf( '<a href="%s">%s</a>', esc_url( get_the_permalink() ), esc_html( get_the_title() ) );
}
echo $saved_html;
You could also use PHP’s output buffering if you prefer, but sprintf()
is the quickest/simplest implementation.
Related Posts:
- echo var into wp_query
- WP query with variables gives no result for specific user
- Query multiple custom post types in single loop
- Pass a PHP variable to another file
- Build a content and excerpt grid loop with paging and options for # of posts
- Using Cookie Data For WP_Query Loop
- wp_delete_post deletes all posts instead of just expired ones
- Trim first 2 words of the exceprt
- Display Post co-authored in Author Page
- Sorting Posts | Alphabetical Order
- Looking for most performant way to execute several similar WP queries within shortcodes
- Locating Global Variables
- Pass PHP variable to JavaScript without inline JS
- Count the number of times the search form template gets included on a page
- Get the last month with posts using a recursive function…
- How to query for pages/post depending on slug?
- WordPress custom slug (endpoint) and compare all links
- How to put a variable in a instance in the widget
- get_term_link() returns correct – But illogical error-messages disturb
- How to use an associative array in post__in with WP_Query?
- Recent Posts slider, using WP_Query(), loads duplicate sliders
- How to use $query->set with post__in?
- Why do WP_Query results change after updating unrelated Advanced Custom Fields (ACF)?
- Display a specific category of products in shop page and disable code for specific actions
- WordPress, AJAX and pre_get_posts using conditional tags
- get_posts() and WP_query limits ‘AND’ conditions to a maximum of 6 for meta value queries in WordPress
- How to modify a global variable in a function and use it on another function?
- Exclude posts based on meta value
- Custom query, checking values of multiple meta keys
- Remove echo from shortcode
- How Can I use WP_Query to Only Display 1 Post from Custom Post Type if Query Returns Posts with Matching ID in Custom Field
- Fatal error: Call to a member function query() on a non-object
- Is it possible to retrieve all posts with a certain value for metadata?
- unable to use ‘new WP_Query’ in AJAX call
- Creating WordPress Shortcode with Variable
- How to put a form with php code into a variable or shortcode?
- Trouble inputting variable into WP query
- Array to string conversion on array_map
- Target post_date_gmt if it’s empty or not set
- How Do I Find & Remove H1 From Site-Title-Wrapper? [closed]
- Get all posts as an array ID => Name
- How to hide posts of a specific custom category in WordPress?
- javascript variable to wordpress php variable
- Custom query vars filters problem with pagination
- WordPress meta_query >= &
- WordPress WP_Query without query GET parameters
- Do not duplicate posts with multiple categories in multiple loops
- Dividing the loop to style post differently
- Displaying Event within two given time frames
- Remove Post if Advanced Custom Field is checked to fix paging
- displaying a fall back query if there’s nothing in the post-type category
- PHP variables in a post?
- Search AJAX Filters – Multiple Query Loops Into One Loop (Optimization)
- WP_Query: How to get results from both meta_key options?
- Output product category link from WP_Query
- Isn’t Returning Value While Using SELECT COUNT(*) FROM {$wpdb->prefix}
- wp_query with multiple arguments with AND
- Passing in MySQL prepare statement parameter separately throwing error
- How to send a php var to jQuery Ajax call with two different file
- Nested Queries of decreasing specificity
- Wrong pagination results, page counter resets when navigating
- Unable to display multiple parameters from url by javascript through shortcodes
- Is it possible to order posts using multiple meta queries, i.e. show posts from first meta query, then the second?
- Load more posts using AJAX based on posts inside WP_Query
- in_array not working on dev server but works on localhost
- Custom search query on WordPress page not working
- WP query posts BUT show the most recent and one per author
- Define global custom post
- How to select post ID for given parent name with nested prepared queries?
- posts_per_page displays only 2 posts instead of 4 posts
- Best way to use variables in multiple functions?
- Need to show 7 posts from actual date
- Add code to the header of posts by particular author
- Search for single post by 2 tags
- Paged WP_Query is quitting on page 3
- Get Child of Child Pages in custom Menu
- How to change the value of a variable using input field?
- Meta query not showing result properly
- Woocommerce Related Product by 2 taxonomy
- Help on Wp_query to print an term
- Add class to all meta boxes for a custom post type
- making a search.php query
- Passing parameter from the current URL to redirect to another url
- Pulling a variable into the wp_nav_menu function
- Efficient way of querying for a “fallback” post?
- Can’t get data from audio media
- Create a hierarchical loop at predefined markup requirements
- WP_Query count of different meta key values [duplicate]
- Query seems to be duplicated
- Create and update Custom Cookie based on multiple GET and POST Values for first time visitors?
- Keeping the previous get value and add another value when submitted
- ACF allow zero as a value
- Can’t get options with $data[‘variable’]
- Pagination in category.php not functioning
- Fix incorrect related posts code snippet
- wp_query on search results page showing all results every time
- How to use a conditional statement in a post loop but not count towards the “posts_per_page” if false
- wp_logout action hook is not firing
- How to make posts_like_dislike reference current post when iterating?
- How to get default variation ID (woocommerce)