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.