Function to grab the ID of a custom taxonomy

I am stuck on this one and would be grateful if someone pointed out what I am missing.

Here is my situation: I’ve got a custom taxonomy called venues and I’ve added custom meta to that taxonomy via a custom table called $wpdb->venuesmeta which looks like this:

meta_id venues_id meta_key meta_value
1 4 address 9009 W Sunset Blvd
20 15 address 1401 Santa Monica Blvd

Just a quick two cell example (there is more in there).

Ok. So in certain circumstances, or rather most of the time I am querying the table if only pulls the first cell which happens to be 4.

I created this function which fixed the problem most of the time:

function get_venues_id_by_key( $meta_key ) {
	global $wpdb;
	
	$venue = $wpdb->get_var( $wpdb->prepare( "SELECT venues_id FROM $wpdb->venuesmeta WHERE meta_key = %s", $meta_key ) );
	
	if ( $venue != '' ) {		
		$venue_id = (int)$venue;
		return $venue_id;
	}		
	
	return false;
}

Where I am using it like $address = get_metadata( 'venues', get_venues_id_by_key( 'address' ), 'address', true );.

But in the front end when on a taxonomy in venues like /venues/$tax_name/. It’s still querying the first cell and not the current ID.

So I thought I need to compare the venuesmeta table to the term_taxonomy and can’t seem to get that code working proper. The term_taxonomy table would be:

term_taxonomy_id term_id taxonomy description parent count
3 3 venues 0 1
4 4 venues 0 3

Just a quick two cell example of the term_taxonomy (there is more in there).

So in order to to pull in the $tax for ID of 3, I thought to do a compare of the above function to the term_taxonomy table, but only got to this function, guess this is where i need help..

function get_venues_id_by_key_match( $meta_key ) {
	global $wpdb;
	
	$venue = $wpdb->get_var( $wpdb->prepare( "SELECT venues_id FROM $wpdb->venuesmeta WHERE meta_key = %s AND term_id FROM $wpdb->term_taxonomy WHERE taxonomy = venues", $meta_key ) );
	
	if ( $venue != '' ) {		
		$venue_id = (int)$venue;
		return $venue_id;
	}		
	
	return false;
}
Default image
Austin
πŸ’πŸ½β€β™‚οΈ Husband to Jeana. ⚾️ Dodgers obsessed. πŸ† World Series Champs. πŸ’» Senior WordPress engineer @ Beachbody. πŸŒπŸΌβ€β™‚οΈScratch golfer. 🌱 Hydroponic Gardner. 🍿 Plex nerd. πŸš™ '15 WRX & '66 Corvette C2. 🐢 Missing my love Keiki; RIP 😒. Follow me on Twitter @TheFrosty & Instagram @TheFrosty.
Articles: 285

2 Comments

  1. I’ve made a small mod to the $venue variable (also renamed it for no apparent reason).

    $tax = $wpdb->get_var( 
    		$wpdb->prepare( "SELECT term_id FROM $wpdb->term_taxonomy WHERE taxonomy = %s AND venues_id FROM $wpdb->venuesmeta WHERE meta_key = %s", 
    			get_query_var( 'taxonomy' ),
    			$meta_key
    		)
    	);
    

    This is still returning false. As you can tell MySQL queries are not my strong point. So I am missing something here that compares the two tables to the same ID. Maybe I’ll get it soon.

  2. From initial looks, I think I might have found my answer to get the term_id. To bad it only took ALL day, and way to many hours to list.

    $wp_query->get_queried_object_id();
    

Leave a Reply

%d bloggers like this: