There is really no reason to use raw sql to query posts. You can accomplish just about any type of query using the WordPress API. For full reference see the codex, Function Reference query posts. Try the following:
global $post;
$args=array(
'post_type' => 'tutorial',
'orderby' => 'comment_count',
'posts_per_page' => 10
);
$posts= query_posts($args);
foreach ($posts as $post) :
//do stuff
endofforeach;
Related Posts:
- Problem with ‘post__not_in’
- Sort X categories by last update and show image
- using post__in allow duplicate post id
- Using dynamic conditions in ‘posts_where’ filter
- How to limit search to first letter of title?
- Counter code for paginated category pages in wordpress
- WordPress Search Filter Only for Page with Child of Child of Child of Child of Child
- pagination in author.php template returns 404 error
- How to modify the query to exclude posts by slug?
- Adding Variables to post query
- Tricky WP Query
- query_posts() ALWAYS displays something?
- Using ajax with paging and a custom sub-query
- Exclude posts by post meta value
- Can I use WP_Query one time and then filter the results multiple times
- Different layout based on post amount?
- How to solve this without flushing the rewrite rules for each post query the visitor triggers?
- How to check a list or feed of all posts under a category and tag?
- Best Query for blog posts
- Adding Category Argument into an Array for WordPress Query
- When should you use WP_Query vs query_posts() vs get_posts()?
- How to Get All Posts with any post status?
- posts_per_page no limit
- How can i get count from query post
- query_post by title?
- Why query_posts() isn’t marked as deprecated?
- Alternative to query_posts for main loop? [duplicate]
- Wp get all the sub pages of the parent using wp query
- how to query posts by category and tag?
- Using WP_Query to Query Multiple Categories with Limited Posts Per Category?
- Get the ID of the latest post
- How to query for most viewed posts and show top 5
- Order by meta value or date?
- How do I query by post format in WordPress 3.1
- Display posts of the last 7 days
- Is there a way to exclude the content from the post variable to save on RAM usage?
- Filtering posts by post meta data
- WP_Query vs get_posts
- How to query_posts using meta_query to orderby meta_key AND have a secondary sort by date?
- Query posts by custom taxonomy ID
- Display/query post formats
- How to query post by user role?
- How to return results of a get_posts() in explicitly defined order
- Help to condense/optimize some working code
- How to set posts per page using WP_Query()
- query_posts exclude a meta key
- How do I create a random post that will last for a day
- Ensuring sticky posts are retrieved first (without using two queries)?
- Determine if more posts are available than was asked for in `query_posts()`?
- Modify main WordPress loop with a parse_query filter
- query_posts() in function makes global $wp_query out of sync?
- Limiting query_posts to 1, regardless of sticky post?
- How to make “sticky” pages (and query by them)
- Query posts: how to exclude results if post is in multiple categories
- Query Custom Meta Value with Increment
- query_posts ->using meta_compare / where meta value is smaller or greater or equals
- Is `query_posts` really slower than secondary query?
- How to order posts by descending comment count on taxonomy page?
- Are there any scenarios where the query_posts may be used?
- Alter query on edit.php
- Get posts by meta data OR title
- Sort posts alphabetically by custom field value, insert divider between different letters
- Is it better practice to use query_posts, WP_Query, or get_posts to create various custom loops within a Page?
- WP 5.8 “Query Loop” block: where to place custom query?
- Perform query with meta_value date
- WP_Query ordered by custom field that is a date string?
- Custom Post Type “Event”: chronological list of recurring events
- Pagination on archive.php page
- WordPress Custom Query
- Best way to load page content in Fancybox popup?
- How should I intercept the main query and inject custom join / order by / group by criteria
- advice on creating a ‘related posts’ query like the one used on stackexchange
- order post my meta value m/d/y format with year as included value
- Order by meta value, pro first, then free
- Having trouble generating pagination links on custom query
- How to create an attachments archive with working pagination?
- _wp_page_template to dynamically use template
- How to order posts by modified date without using ‘query_posts’?
- Help altering a query to exclude all but standard post format
- List users with the most total posts view
- Schedule Sticky Posts
- Use union/intersection query_posts variables in uri request parameter form?
- query_posts doesn’t order by title
- query_posts and pagination, still stuck after much research
- Query posts by taxonomy term name
- Sorting problem with ‘query_posts’ funcion in wordpress. Sort by custom field not working
- Total Count of Posts NOT in Selected Categories?
- Custom query with query_posts doesn’t show post without certain meta_key
- how could I get the pagination as I want to when query posts using get_posts function
- Post Title displaying but not in the wrapped HTML I need
- Author List page: Exclude based on last post date
- Replace query_posts with pre_get_posts
- How to sort by meta value?
- query_posts sort in multiple directions
- Filter query_posts by tag slug on “Tag Archive” page (when tag is 2 or more words)
- get_post_meta causes database queries
- How to display liked posts of current user in wordpress?
- Problem with single-page for my custom post
- Order post by year DESC and month ASC
- How to ensure that is_search() return false after query_posts