WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've been trying to figure this out for a while now. So I'm trying to add a custom field call "counter" to every single post. I try simply inserting the field "counter" to the post table. Every time a user clicks a button on the post, it increments the counter So far so good.

But now the tricky part is being able to get back the information with WP_Query. I want to do a custom query for posts where the counter is greater than 0, loop through the posts, and display them with template tags.

If i were to write an sql stamens it would look like this

$query = "SELECT * FROM $wpdb->post WHERE counter > 0";

But I can't do a normal SQL query because then I won't have access to template tags such as Wordpress's "show next page | show older page" template tags.

I'm not looking for a plugin but I'm trying to find a way that i can add a field to the post table and be able to query the posts base on the custom fields, loop through the result, and display the information about the post. If you guys have any suggestions, I would really appreciate it.

Note: Am i suppose to add the custom field to post meta? Looking at the SQL table, it doesn't seem like a right place to do it...

share|improve this question

Adding a column to the posts table is a bad idea, this is indeed what Custom Fields (Post Meta) are for. Query and sort your posts based on the values of your custom field with WP_Query and a post meta (custom field) query:

$query = new WP_Query(
        'meta_key' => 'counter',
        'meta_value' => 0,
        'meta_compare' => '>'
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.