You don’t need to use query_posts
function. It could be done without it. Use setup_postdata
function to set up global post data.
$sql = "
SELECT
p.*
FROM
orders_items oi
INNER JOIN products pr
ON pr.`post_id` = oi.`product_id`
INNER JOIN posts p
ON p.`ID` = pr.`post_id`
AND p.`post_type` = 'product'
GROUP BY oi.`product_id`
ORDER BY COUNT(oi.id) DESC
";
echo '<ul>';
$result = $wpdb->get_results( $sql );
foreach ( $result as $post ):
setup_postdata( $post );
?><li>
<a href="https://wordpress.stackexchange.com/questions/79989/<?php the_permalink() ?>"><?php the_title() ?></a>
</li><?php
endforeach;
echo '</ul>';
Related Posts:
- WordPress Paginate $wpdb->get_results
- Custom Search Query
- Add field to posts (checkbox) that only one post can have
- Create subpage /user/ or /my-profile/ like /author/ with additional query like /user/user123
- Custom Queries – Child Categories not showing up
- Order post by year DESC and month ASC
- Retrieve and display data from custom db table in admin area?
- How to find if a post with custom_field == X exists?
- Custom search page and search by title, content and tag
- Modifying loop on a custom page, strange behaviour
- Adding Variables to post query
- Filter my home page posts by category or by tag
- Query posts by category AND custom field, then ORDERBY another custom field – help!
- How I display post of the post type group by year of post?
- Load more posts with multiple queries
- Hi, I wanted to make custom search with multiple fields
- $wpdb->replace: WHERE?
- wp_query get post custom taxonomy in search.php
- How to get Related Custom Posts?
- Order posts with a custom field but also without
- Save title as custom field on custom post type
- Add content in between of foreach
- Custom Loop Query For Search Page
- Why in this archive page that call query_posts() function show only the last 10 posts?
- Custom SELECT Query With Multiple Meta Values (Ordering Problem)
- Query for specific custom field
- Display posts by combining multiple meta key
- Custom select query two tables by a meta key
- Make tag archive display post are ordered by post format
- Custom Same Page Search
- Fatal error Call to a member function insert() on a non-object [closed]
- custom user meta query
- Recent activity query WordPress
- How to query term_id of a Custom Taxonomy by name
- query_posts meta_key with current date
- WP Search Form Query: Add Author’s posts into search query
- query variables for custom taxonomies
- display text generated via custom get query string
- Custom query pagination with php parameter as arg problem
- How to automatically pull an information from a form field onto an other page?
- Query Posts to display all posts with category “custom field value”
- Query 1 custom post type, each day, alphabetically
- Multiple Taxonomies Using Custom SQL Query
- Search a meta field for a value in all posts
- Custom WordPress SQL Query 4 posts per custom taxonomy
- Custom Query Arguments
- Custom Query for Taxonomy
- Include plugin´s table in custom query
- User Query Multiple Orderby Clause
- Advanced Custom Fields in WP_Query: Href Returning Empty
- When should you use WP_Query vs query_posts() vs get_posts()?
- How to: Easily Move a WordPress Install from Development to Production?
- Is there a flowchart for WordPress loading sequence?
- Essential technical features for high-end WordPress web hosting? [closed]
- How to remove admin menu pages inserted by plugins?
- How to put logs in WordPress
- How to get the Date Format and Time Format settings for use in my template?
- what is the correct way to compare dates in a WP query_posts meta_query
- Nested meta_query with multiple relation keys
- What SQL Query to do a simple find and replace
- Where are Additional CSS files stored
- Best Practices for Regression Testing WordPress Websites?
- Remove wrapping div and ul from output of wp_nav_menu
- What Is The Use Of map_meta_cap Filter?
- get post author id outside loop
- Custom Walker: how to get ID in function start_lvl
- Creative uses of WordPress [closed]
- How to *remove* a parent theme page template from a child theme?
- How do I make my child theme re-apply the settings that were customised when its parent was active?
- Multiple Inputs in a Customizer Control
- New WP_Customize API – how does it work under the hood?
- Embedding a SOAP Client into a WordPress Plugin?
- Is there anything that Joomla or Drupal can do that can’t be done in WordPress? [closed]
- Update widget form after drag-and-drop (WP save bug)
- Data sanitization: Best Practices with code examples
- Wp get all the sub pages of the parent using wp query
- How to benchmark a WordPress installation? [closed]
- Should I delete the default themes?
- Moving WP install from local to live, what about wp_posts GUID?
- Modified wp.media.view.Settings.Gallery in Backbone JS, but editing doesn’t work
- Running Gutenberg React in Development Mode
- Prevent WordPress from automatically installing a new theme each year
- Can we use one WordPress installation for multiple databases, domains and content directories
- Is there a blank theme framework compatible with WP 3.0? [closed]
- Plugin SVN & update API – how are plugins identified?
- Get the ID of the latest post
- How to query for most viewed posts and show top 5
- Users with custom roles not showing in post author select box
- How to use more than 256MB of memory in the admin?
- How do I query by post format in WordPress 3.1
- How do register_sidebar() and get_sidebar() work together?
- How do you get formatted content of a post using the WordPress API?
- Localization: I want the backend: english and frontend in defined language
- WP_Query vs get_posts
- Are there any forks of WordPress (and what is different about them)?
- How to Change 404 page title
- Make Custom Metaboxes Collapse by Default
- Custom maintenance page
- Add button to TinyMCE bar without creating a plugin
- Why do I get the timeout warning?