Jump to content
Jim R

Older WP plugin I created no longer works...

Recommended Posts

This has probably been about six years ago, and I had a lot of help here with this plugin.  I'm looking to use it again for another site.  


The intent of the plugin was with each new Tag (basketball player names) match the slug in wp_terms with wpSlug in a custom data table (same database), then copy the term_id from wp_terms to wpID in the custom data table.  This serves as a trigger.


It's no longer working, but it's not triggering any readily visible errors.  


(I'm pretty sure my method at the time was pretty convoluted.  If it helps, right now I just wp_terms table to tell my a_players table a matching slug exists.)

// Your custom table name

add_action('save_post', '__and_upd_other');
add_action('edit_post', '__and_upd_other');

function __and_upd_other( $post_id ) {
global $wpdb;

if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return $post_id;
if ( 'page' == $_POST['post_type'] ) {
if ( !current_user_can( 'edit_page', $post_id ) ) return $post_id;
else {
if ( !current_user_can( 'edit_post', $post_id ) ) return $post_id;
$post_tag = ($_POST['tax_input']['post_tag']) ? $_POST['tax_input']['post_tag'] : '';

if('' != $post_tag) :

$post_tag = explode(',',$post_tag);

$ptc = 0;
// Build string of OR matches for the query
foreach($post_tag as $tag) {
if($ptc == 1) { $tt = "terms.name = '$tag'"; } 
else { $tt .= " OR terms.name = '$tag'"; }
// Get the tag ids and slugs for the saved post
$tags = $wpdb->get_results("
SELECT terms.term_id, terms.slug 
FROM $wpdb->terms as terms
JOIN $wpdb->term_taxonomy as tax ON tax.term_id = terms.term_id
JOIN $wpdb->term_relationships as rel ON rel.term_taxonomy_id = tax.term_taxonomy_id
WHERE rel.object_id = '$post_id'
AND tax.taxonomy = 'post_tag'
AND $tt
", ARRAY_A);

// Create 2 empty strings
$mstr = $lstr = '';

// Build a case string for the next query
foreach($tags as $t => $tag) {
$mstr .= "when '$tag[slug]' then '$tag[term_id]' ";
$lstr .= "'$tag[slug]',";
// Unset unused data

// Remove the last comma off the second string
$lstr = substr($lstr,0,-1);

// Create the a nifty query to do multiple updates in one query
$wpdb->query( $wpdb->prepare("
SET ".S_DB.".wpID = case wpSlug
$mstr end
WHERE ".S_DB.".wpSlug in(%s)"), $lstr);
//old line before switching due 3.5 upgrade  WHERE ".S_DB.".wpSlug in($lstr)"));

Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.