0
votes

What I'm trying to accomplish?

Getting the count of something, more specifically when the user presses a button on a specific post. I need an SQL statement to keep track of that count(don't worry I'm going to get more specific).

Table structure

This is a normal WordPress table structure where I'm focusing on the following tables:

  • wp_posts --> ID, post_title, guid
  • wp_term_relationships

| object_id | term_taxonomy_id | term_order |
|-------------|--------------------------|----------------|
|-----6------|------------1-------------|---------0-----|
|-----22----|-------------2------------|----------0-----|
(table formating, I tried)

  • wp_term_taxonomy

| term_taxonomy_id | term_id | taxonomy-----------| description | parent | count |
|--------------------------|-----------|------------------------|----------------|----------|-------|
|-------------1----------- |-----1----|---------0-------------|----------------| -----0---|---10--|
|------2-------------------|-----2----| button action's tax |-------------- |-----0---|----4---|

What I noticed

I'm just getting started with wordpress, and I'm unfamiliar with the table structure but I noticed that the buttons action in my plugin created an iteration in wp_term_relationships --> term_taxonomy_id so I built an SQL statement around that where I stored the post Id as $post and compared it to the object_id in the wp_term_relationship table. Got the specific term_taxonomy_id for that respective post ID then I subtracted it by one since that term_taxonomy_id is initialized as one when a post is created.

Thought I solved it this way

global $wpdb;  
$ID = get_the_ID();
$count = $wpdb->get_var("SELECT term_taxonomy_id FROM wp_term_relationships WHERE object_id = '".$ID."' ORDER BY term_taxonomy_id DESC LIMIT 1");
$the_count = $count - 1;
echo $the_count;  

Problems/Questions

I was told over and over again that this will not work if something changes in the database. I just don't know what that something is, and what kind of statement I need to avoid this problem.

2

2 Answers

1
votes

Don't mess with the taxonomy tables. You'll break stuff unless you're very careful. Plus, that's not where you want to store this information.

You want to keep and update a hit count as an attribute of a post. Attributes go in the wp_postmeta table, not the taxonomy subsystem.

You don't have to write a query to do this. You can simply use the get_post_meta() and update_post_meta() functions.

Something like this should do the trick for you.

$hit_count_key = '_hit_count';
$hit_count_val = get_post_meta( $post_id, $hit_count_key, true );
if ( $hit_count_val === false ) $hit_count_val = 1;
else $hit_count_val++;
update_post_meta( $post_id, $hit_count_key, $hit_count_value);

(I haven't had a chance to debug this code. But you get the idea.)

The leading underscore on the key means that the attribute isn't displayed in the post edit screen. If you want the attribute displayed, don't use the underscore.

0
votes

you are confused, if i`m not wrong, you want to create a counter (How many people are clicking in X post, wright?).

  1. In table wp_term_taxonomy, the 'count' row, it's not the times a user has click on the post.those numbers are the number of terms asociate to a Taxonomy.

  2. If you want a counter there is a fast way or a 'Hard way'.

  3. Hard way: create your own plugin and use wp_options to save your data