Skip to content

Instantly share code, notes, and snippets.

@TanvirAmi
Created January 19, 2018 04:37
Show Gist options
  • Save TanvirAmi/6f7759564d069f8f10b50fea51bbac94 to your computer and use it in GitHub Desktop.
Save TanvirAmi/6f7759564d069f8f10b50fea51bbac94 to your computer and use it in GitHub Desktop.
<?php
/*
* Returns post IDs in a term with a given ID
*
* @param int $term_id(Category ID, Tag ID)
*
* @return array|int Post ID(s)
*/
function get_post_id_by_term_id( $term_id ){
global $wpdb;
$ids = $wpdb->get_col( $wpdb->prepare( "SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $term_id ) );
if( count( $ids ) > 1 )
return $ids; // return array
else
return $ids[0]; // return int
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment