Quick Tip: “Popular Posts By Comment Count” SQL Query in WordPress

You might have noticed that the Tuts+ sites have a section on the home page where we list the most popular posts of the month, according to comment count. While there are numerous plugins available, it’s always best to write the code yourself if you can. Too much abstraction is never a good thing! Luckily, once we learn how to query WordPress’ database, tasks like this become a cinch!

Prefer to watch this video on Screenr?


The SQL Query

To query WordPress’ database, we can use the $wpdb->get_results() method. As the parameter, we simply pass a SQL query. In this particular example, let’s say that we only want to display a list of popular posts within our sidebar. We can use the following query:

$pop = $wpdb->get_results("SELECT id, post_title, comment_count FROM {$wpdb->prefix}posts WHERE post_type='post' ORDER BY comment_count DESC LIMIT 10");

id, post_title, and comment_count our columns within the database.

MySQL DB

Because WordPress allows you to set a custom prefix for your database tables, we need to fetch that prefix dynamically, by using {wpdb->prefix}posts. In this case, that results in “wp_posts”. Next, we must ensure that we only display posts, and not anything else, such as attachments or pages. Finally, we ORDER BY comment_count. That’s the point of all this right? Display the most commented postings?

Now that our SQL query has been executed, we can simply use a foreach statement to filter through the results, and display them on the page.

$pop = $wpdb->get_results("SELECT id, post_title, comment_count FROM {$wpdb->prefix}posts WHERE post_type='post' ORDER BY comment_count DESC LIMIT 10");

foreach($pop as $post) : ?>
<li> <?php echo $post->post_title; ?> </li>
<?php endforeach; ?>

Note that the $post variable will have access to any of the columns that we fetched. In our case, we’d have access to:

  • $post->id : The id of the post
  • $post->post_title : The title of the post
  • $post->comment_count : The number of comments for that particular post.

If you need the permalink, you can either also SELECT from the “guid” column, or you can just use the get_permalink($post->id) method, and pass in the id of the post accordingly.

Once you’ve learned how to directly interact with the WordPress database, you then have a lot of power at your disposal; this is only the tip of the iceberg!

Any questions? Thanks for viewing or reading!


WANTED: 1000+ Emails of Mobile Phone Professionals by MisterZ

This will be a trial run for future work. Who I’m targeting: -Mobile phone stores -Mobile phone distributors -Mobile repair stores -Mobile repair professionals -Mobile unlocking professionals -Mobile phone parts suppliers Must have NO MORE THAN a 10% bounce rate… (Budget: $30-250, Jobs: Advertising, Bulk Marketing, Internet Marketing, Leads, Marketing)

Long-Term Article Rewriting, Native English Writers by jbventures

Hi, I’m looking for serious LONG-TERM article writers who will receive a very regular flow of daily work. These won’t be original articles – rather they’ll be rewrites. I’ll be giving you an article, and I’ll be asking you to rewrite each paragraph 3 times… (Budget: $250-750, Jobs: Article Rewriting, Articles)

Php Page Generator

I would like a simple php tool designed that will generate a multi-page website for me that will have just a few elements that I can specify for each page.

Basically I would like a tool that has the same framework as www.bargainpurses.info that I can use to build other websites and be able to specify new keywords for.

I can provide the php files for that site so that you can see how it all goes together. I would just like it setup so that I can enter keywords for each main page as well as keywords for the subpages of each main page. the subpages would be linked on the left like you will see on the example website. This is a two level system, but should not be difficult.

For exmaple The home page would have a navigation bar that would link to the main topics such as different brands of purses. Then on each of the dynamically generated pages of the main topics there would be a different set of navigation links that I would specify for the subpages. You will know what I mean by looking at the site I posted above.

The tool to generate these sites should be something that is uploaded to a server for each site and the keywords and pages would be entered on an admin page. The simpler the interface, the better.

It would look something like:

Keyword 1:
sub-keyword 1
sub-keyword 2
sub-keyword 3

Keyword 2:
sub-keyword 1
sub-keyword 2
sub-keyword 3

etc….

The main objective is to be able to build sites very similar to www.bargainpurses.info FAST. The framework should be very SEO friendly as well.

There should be options to turn on Adsense ads and the functionality to display E-bay Ads from the EPN network should be built in.

Any questions or clarification, send me a message.

Happy bidding!

WordPress Style Website ++

I need a wordpress style website that has a header (for a graphic,web name etc)

There needs to be a spot for an ad/ banner across the top and then some banners or ads down the right side and perhaps one at the bottom of every page.

People can post anonymously or register. registering would allow them a basic page which would have the option to post their full face picture if they wanted, upto to a certain KB size.

This page would specific details like age, country, city, sex, and name with a spot for a link or 2 to a website or social page.

Every page would be the same for those who ‘register’, anonymous users would simply get to post a pic of their eyes.

The details such as age, country, city etc..have to be searchable so that they can be brought up on demand when searching country etc.

The user simply is posting a picture. I need an image uploader that will allow them to upload their pictures(upto a certain size in kb or mb) There is a somesort of ‘template'(ex. 450x75px) that they place over their picture shown so that it will ‘crop’ the picture to show only what lies within the area of the ‘template’.

Additional details will be provided*

Each post is numbered automatically and shows the users ‘post’, country, city, age and a voting system which keeps the most popular pic up in the respective order on the pages.

every post will have social buttons like digg, facebook, twitter, redditt, stumbleupon, maybe code to allow the post to be email or posted on a blog?

Maybe somesort of widget showing the top ranked pic
Maybe some sort of way for people to link to their friends post (ex picture)or link pictures to other pictures that look similar??

Some of these ideas are just thoughts right now–but this is what i am looking for.

**This needs to be easily administered like wordpress and for removing posts easily etc…adding and changing advertisements/banners just like wordpress.

The main portion of this wordpress layout should be simple for someone who knows their stuff well.

Please feel free to ask questions for clarifications—this shouldn’t be a big deal for the right person.

PHP Script for EXCEL Spreadsheet by iStryker

I need to to take a excel spreadsheet file and convert it into a csv to be able to import it into Drupal. The excel spread file has cells that have to much information that needs to be separated (see attached file) I would like a script that i could run again and again on new files… (Budget: $30-250, Jobs: Drupal, MySQL, PHP)

Complicated Website

I’m not going to say too much in the description, but I need a programmer who has experience in making websites and in website design. This particular website is going to be tricky to make (I believe), so only take the job if you know you have extensive knowledge in this field of programming. In short, It will be a website that allows individuals to sign up and become users, and then these users will be able to create their own individual pages. There are many more details, however, if you think you can tackle this sort of project, accept the project offer and I’ll email you with more information!