2
votes

First and foremost, I need to stress how greatly I appreciate anyone who takes the time to respond - In advance, thank you, thank you, THANK YOU!

I feel pictures are of great service to explaining things, so to start off, here's something of an illustration of the issue:

http://i.stack.imgur.com/b9OXR.png

Preface:

  • My site has about 400 Posts under the "dir_listing" Custom Post Type
  • Each "dir_listing" Post can be associated with Terms from the "listing category" Custom Taxonomy and/or "listing_region" Custom Taxonomy
  • I have about 210 "listing_category" Terms, and 155 "listing region" Terms

The Issue in Summary:

When Publishing or Updating a "dir_listing" Post Type, sometimes the Term IDs get recorded into the 'term_relationships' table without issue, and other times are recorded erroneously.

Even so, when I go back to edit one of the "dir_listing" Post Types the checkboxes for the desired Parent/Child Terms are correctly marked.

Actions that could be Related?

  • A number of the "listing_category" terms have been shuffled around. (e.g. A Parent term has become a Child term, or vice-versa, or a Child term has been moved to another Parent.)

  • Both Parent & Child Terms have been renamed without making any changes to the "dir_listing" Post types. (I don't think this matters, since Posts are supposed to be related to Terms by ID #)

  • A number of Parent Terms and Child Terms may have been deleted by another Admin-level user. This also seems to happen with Child-Terms of the "listing_regions" taxonomy.

How I've Investigated / Tried to Fix It:

  • Extensively searched WordPress trac (No similar issues reported)

  • Searched Google for things like *"wordpress tax_input bug"*, "wordpress taxonomy id bug", "wordpress custom taxonomy bug", etc. and found no matching issues

  • Disabled all plugins, custom rewrites, and other taxonomies

  • Ensured the checkbox inputs had the correct Taxonomy as their 'name' and Term ID as their 'value'

  • Hacked the /wp-admin/includes/post.php core file to try to fix it myself. (No luck.)

  • Posted issue to [WordPress "How-To and Troubleshooting" forum][3] this morning (No replies)

  • Posted issue to Reddit /r/web_design this afternoon (Also no replies)

I've spent the better part of 8 hours trying to determine the cause of this, or if I'm missing a step by using a custom MySQL query to directly get a list of Posts related to a particular Term ID.

Again, any thoughts or suggestions anyone might have are VERY MUCH appreciated - Thanks!

1
Are you still trying to fix this? If so, I might be willing to take a closer look.tollmanz
What table are you showing in the screenshot? I've never seen a similar one in wordpress db. What version of wordpress do you have? Did you use a plugin to create thoses taxonomies?Joeyjoejoe

1 Answers

-1
votes

This info might help you with how the term_id and and term_taxonomy_id work. It seems like there are a lot of conditions that effect the final outcome of how the ids are set.

See the full reference here: http://phpxref.com/xref/wordpress/wp-includes/taxonomy.php.html#wp_insert_term

Also have a look at this trac ticket regarding the need for a posts relationship table and some of the use cases.

get_term($term, $taxonomy, $output = OBJECT, $filter = 'raw')   X-Ref
Get all Term data from database by Term ID.

The usage of the get_term function is to apply filters to a term object. It
is possible to get a term object from the database before applying the
filters.

$term ID must be part of $taxonomy, to get from the database. Failure, might
be able to be captured by the hooks. Failure would be the same value as $wpdb
returns for the get_row method.

There are two hooks, one is specifically for each term, named 'get_term', and
the second is for the taxonomy name, 'term_$taxonomy'. Both hooks gets the
term object, and the taxonomy name as parameters. Both hooks are expected to
return a Term object.

'get_term' hook - Takes two parameters the term Object and the taxonomy name.
Must return term object. Used in get_term() as a catch-all filter for every
$term.

'get_$taxonomy' hook - Takes two parameters the term Object and the taxonomy
name. Must return term object. $taxonomy will be the taxonomy name, so for
example, if 'category', it would be 'get_category' as the filter name. Useful
for custom taxonomies or plugging into default taxonomies.

param: int|object $term If integer, will get from database. If object will apply filters and return $term.
param: string $taxonomy Taxonomy name that $term is part of.
param: string $output Constant OBJECT, ARRAY_A, or ARRAY_N
param: string $filter Optional, default is raw or no WordPress defined filter will applied.
return: mixed|null|WP_Error Term Row from database. Will return null if $term is empty. If taxonomy does not

get_term_by($field, $value, $taxonomy, $output = OBJECT, $filter = 'raw')   X-Ref
Get all Term data from database by Term field and data.

Warning: $value is not escaped for 'name' $field. You must do it yourself, if
required.

The default $field is 'id', therefore it is possible to also use null for
field, but not recommended that you do so.

If $value does not exist, the return value will be false. If $taxonomy exists
and $field and $value combinations exist, the Term will be returned.

param: string $field Either 'slug', 'name', or 'id'
param: string|int $value Search for this term value
param: string $taxonomy Taxonomy Name
param: string $output Constant OBJECT, ARRAY_A, or ARRAY_N
param: string $filter Optional, default is raw or no WordPress defined filter will applied.
return: mixed Term Row from database. Will return false if $taxonomy does not exist or $term was not found.

get_term_children( $term_id, $taxonomy )   X-Ref
Merge all term children into a single array of their IDs.

This recursive function will merge all of the children of $term into the same
array of term IDs. Only useful for taxonomies which are hierarchical.

Will return an empty array if $term does not exist in $taxonomy.

param: string $term ID of Term to get children
param: string $taxonomy Taxonomy Name
return: array|WP_Error List of Term Objects. WP_Error returned if $taxonomy does not exist