Codex has quite an extensive article on this – Displaying Posts Using a Custom Select Query.
In a nutshell:
// retrieve posts using our custom SQL (note that you should query * rather than pick fields)
$posts = $wpdb->get_results($query, OBJECT);
// this is global variable that WP functions rely on for output
global $post;
// loop
foreach($posts as $post) {
// get global stuff filled current post
setup_postdata($post);
// use template tags and profit
the_title();
}
// cleanup after ourselves
wp_reset_postdata();
Related Posts:
- Some doubts about how the main query and the custom query works in this custom theme?
- WP_Query vs get_posts
- Order posts by ID in the given order
- Get the number of posts from the current page results
- WordPress Custom Query to show posts from last x years
- Query Custom Meta Value with Increment
- How to query ‘posts_per_page’ to display a different blog posts index template?
- 2 loops on page – one with orderby rand second orderby date
- Query posts by specific word on title
- Five posts from a category in footer
- Multiple loops without repeating content
- WP_Query condition affects posts_per_page count
- Some doubts about how the main query and the custom query works in this custom theme?
- How do I rewrite this loop as a new WP_Query style-loop?
- Include current post into loop
- How to provide meta_key array to wp_query?
- get_posts output always same post
- How to make posts unqueryable/unpublish posts where ACF relationship field is an unpublished post? [closed]
- Conditional operator OR not working with custom fields
- Get posts that do not have the same tags as current
- Should I reset $wp_query?
- 2 queries with counters
- Paginate pages with dynamic query
- Performance concerns: index.php vs taxonomy-$taxonomy.php
- Determine if ID is page or post and query the ID
- Custom template for password protected page
- Magazine style frontpage with multiple categories/loops and no duplicate posts
- Duplice post with standard WP loop – fixed by using query_posts() instead
- How do I stop the same post showing multiple times in a archive?
- Custom query for custom post type not getting correct post ID
- Problem with my loops
- WordPress For Loop Prints Unwanted Extra Paragraph Element
- How to order posts, that have already been filtered by custom taxonomy, by their category names?
- How to add in WP_Query to every 3 posts displayed? [duplicate]
- query_posts() doesn’t seem to be called in my page
- Some doubts about how the main query and the custom query works in this custom theme?
- Ordering Posts by parent category, name ascending
- Converting an existing query_posts to WP_Query
- Multiple loop for “featured” items returns wrong posts
- Show number of posts AND number on current page (cannot make it work)
- When to use WP_query(), query_posts() and pre_get_posts
- Resetting post data to previous loop in nested loops
- Pagination returns 404 after page 20
- Make loop display posts by alphabetical order
- get query’s query string
- Group posts by custom field
- Transient pagination not working properly
- get_posts() seemingly ignoring post_type
- Woocommerce custom loop to show all the products [closed]
- Query for posts in 2 taxonomies
- How to show the posts of some category first, and then all other
- Pagination stops at page 6
- Paginated HTML Sitemap
- Why does the_content not work like others for a set post id?
- How to limit total number of posts in wp query loop?
- WP Pagination on Posts Search Results Page resulting from AJAX WP Query
- How to vary post loop results layout and resume?
- Get a post from other loop each n-posts in the main loop
- Query last updated posts (posts updated in the last 24 hours)
- WordPress Loop and $post
- How can I create ‘future’ and ‘past’ parameter for restAPI by filtering the CPT custom date field by greater than / less than current datetime?
- new WP_Query(), have_posts() return false in user_register hook
- Excluding pages in WP_query using ACF
- reset to main loop doesnt work
- Post loop for all taxonomy terms
- Query all posts if multiple meta key don’t exist
- Only display post if published in last 24 hours?
- How to remove the most recent post from $the_query
- WP_Query loop else statement not executing
- Check for the main query from the template
- Filter post query to only show direct children of category
- Query_posts works when appending via AJAX call wp_query doesn’t?
- PHP – Loop custom post type categories within jQuery Tabs
- Ordering posts by custom field and grouped by month
- Show single posts date, in a page of posts
- relation OR instead of AND – Filtered term ID’s in loop
- Compare “Main” post ID to ID inside wp_query loop
- filter posts by meta key with pagination
- WP_Query Class custom field parameters
- Add AJAX “Load more” on custom query block
- problem with the loop
- Function the_posts_pagination() not compatible with WP_Query arguments
- Facing problem with tax_query results
- Prevent Duplicate Post Counted by Query
- How to show specify category template for both parent and child category
- Woocommerce featured products query no longer working
- How to query most recent post for each author?
- How to search CPTs in draft using get_page_by_title()
- query all posts published by certain user id
- Custom WP_query and integrating into theme file
- modifying the loop multiple times with arguments passed through ajax to wp_query
- WP_Query causing links to not work
- Iterate through ID’s in loop
- An archive page without post format (just standard post)
- How to organize a WP_Query’s list of posts by category and display category title?
- query post based on comparison
- Pull Instagram images into an existing loop?
- How to write a query-function as a query-shortcode?
- First post outside of loop, homepage only?
- new WP_Query with order args – no more distinction between categories