Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save meetKowshik/d05b512c423eebed0162d7cc3aea79c8 to your computer and use it in GitHub Desktop.
Save meetKowshik/d05b512c423eebed0162d7cc3aea79c8 to your computer and use it in GitHub Desktop.
Get the tags or categories based on post ID from the wordpress db in custom way
//=== here postId is the Post ID which is passing from a shortcode function
function eventsCats($postId) {
global $wpdb;
$get_term_taxonomy_id_q = "SELECT term_taxonomy_id FROM wp_yjigapdowy_term_relationships WHERE object_id = $postId"; // get the term taxonomy id from the db based on product id
$get_term_taxonomy_id_arr = $wpdb->get_results($get_term_taxonomy_id_q, ARRAY_N);
// store term taxonomy id array
$get_term_taxonomy_id = array();
foreach($get_term_taxonomy_id_arr as $taxonomy_id) {
foreach($taxonomy_id as $id ) {
$get_term_taxonomy_id[] = $id;
}
}
// store the term id based on the term taxonomy id from the db
$get_term_id_arr = array();
foreach($get_term_taxonomy_id as $term_taxonomy_id) {
$query= "SELECT term_id FROM wp_yjigapdowy_term_taxonomy WHERE term_taxonomy_id = $term_taxonomy_id AND taxonomy = 'events_dir_tag' ";
$get_term_id_arr[] = $wpdb->get_results($query, ARRAY_N);
}
// store the term id for the next query
$get_term_id = array();
foreach($get_term_id_arr as $term_id) {
foreach($term_id as $id) {
$get_term_id[] = $id;
}
}
// store the term name to show
$get_term_name_arr = array();
foreach($get_term_id as $termId) {
foreach($termId as $id) {
$query = "SELECT name FROM wp_yjigapdowy_terms WHERE term_id = $id";
$get_term_name_arr[] = $wpdb->get_results($query, ARRAY_N);
}
}
// to show the term name on the front end
$term = array();
foreach($get_term_name_arr as $get_term_name) {
foreach($get_term_name as $get_name) {
foreach($get_name as $term_name) {
$term[] = $term_name;
}
}
}
return $term;
}
print this in shortcode function liken this
$term = eventsCats($id);
foreach($term as $name) {
$output .= '<a href="'.$base_url.'/directory-events/tags/'.sanitize_title_with_dashes($name).'">'.$name.'</a>';
if(next($term)) {
$output .= ",";
}
}
$output .= '</span>';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment