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
- how to query posts by category and tag?
- How do I query by post format in WordPress 3.1
- How to set posts per page using WP_Query()
- How do I create a random post that will last for a day
- Query Custom Meta Value with Increment
- Is `query_posts` really slower than secondary query?
- Are there any scenarios where the query_posts may be used?
- Custom Post Type “Event”: chronological list of recurring events
- 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?
- Sorting problem with ‘query_posts’ funcion in wordpress. Sort by custom field not working
- how could I get the pagination as I want to when query posts using get_posts function
- Author List page: Exclude based on last post date
- Replace query_posts with pre_get_posts
- query_posts sort in multiple directions
- Filter query_posts by tag slug on “Tag Archive” page (when tag is 2 or more words)
- Warning: urlencode() expects parameter 1 to be string, array given in and not getting the preffered output
- Show Posts From Same Category OR Same Tag
- Using database meta_values to calculate new post order using pre_get_posts or a ‘request’ hook
- exclude ids through post__not_in
- Taxonomy.php issue with search and filters
- How do I query for a post by custom field?
- Integrating post archive wtih another source and preserving date ordering
- Order posts using a custom array
- How can I sort homepage by a meta value?
- IF.. post meta show… Conditional Tag Help…?
- How do I correctly get all posts within the last year using the query_posts function?
- Show Only Posts Owned By Logged In User + my post
- Custom MySQL Query with logic
- Query last updated posts (posts updated in the last 24 hours)
- order posts by meta value on posts page
- Get post and all posts after it by ID?
- trouble with query to fetch next and previous posts
- How do I make query use exact post title
- WordPress tax_query not showing private posts
- Different Ways to Query Custom Post Types?
- WordPress /page/2 not working
- Simple custom post query for all the posts or many of them not working
- Query Page Content From Theme Options?
- Hook to return true when the_post() is used in a custom while loop
- My post repeats itself on the second page [closed]
- How to remove a plugin filter’s priority on specific loops (custom queries)?
- get_the_title outputs title until spacing; it does not get full length of title
- Counter is skipping post when I still want it visible [closed]
- WordPress wp_get_attachment_thumb_url full
- Why in this archive page that call query_posts() function show only the last 10 posts?
- Display trimmed version of the_post()
- How to show the number of each post has been read in a while loop?
- Modify loop but keep the original query, what am I doing wrong?
- Post per page not working if no category specified
- How to give classname to post if post has no content?
- Query to list all galleries under a parent post?
- How exclude 3 latest posts of a category in query_posts
- query_posts orderby multiple ways
- Pagination linking to first page
- Weird problem on if statement
- Issue with sidebar widgets not showing when I do custom queries
- Why would post_type be ignored in this query?
- query_posts call sets is_home() to true
- Most Effective Method? Exclude Category and Number of Posts Per Page
- Script that gets the first tag of post and query all other posts with the same tag
- Trouble highlighting correct menu parent with wp_nav_menu classes while viewing “single posts”
- Custom query looking at multiple custom fields and properly sorting
- Latest 5 post excerpts from 5 different authors in Sidebar
- post var problem with shortcode loop
- Pre-existing arguments when using queries
- Change pure SQL database query to WordPress post query?
- Help with query_posts function
- Get posts for each user
- Sort / Filter Queries
- How to organize a WP_Query’s list of posts by category and display category title?
- Query Posts From Multiple Post Types
- get_next_posts_link() stops displaying at page 4
- WP-Snap too slow (caused by WP_Query?)
- Multiple post queries -category,posts per page,orderby
- Displaying posts by year
- Including post data in a sidebar occurring outside of and before the Loop
- How do I find the first item in the post array?
- Sort Posts Alphabetically Based on Specific Category (Divi)