I know there is connection between post table with term table but I am unable to figure it out.

wp_terms, wp_termmeta, wp_term_relationships & wp_term_taxonomy relation is a little confusing. So, I am unable to write a query to get term id from post id.

Actual Problem:

I am using this piece of code to get an object and extract term id from that. But for some reasons, it is not working so I want to manually query in database to pull the term id and do rest of process.

get_metadata_by_mid('post', $id);

it returns this object: (on local machine)

stdClass Object
    [meta_id] => 1268   //this id is passed to function like get_metadata_by_mid('post', 1268);
    [post_id] => 222    //want to obtain this ID
    [meta_key] => _menu_item_url
    [meta_value] => 

Returns no result (on server) [so looking for alternative way]


  • get_metadata_by_mid works on local machine but doesn't work on live (i don't know, why whereas I have same version in both environment)
  • If someone can provide alternative way then it would be better than manual query.

3 Answers 3


It is not good practice to use query when you have already builtin functions but for your need.

Try this solution:

global $wpdb;
$meta = $wpdb->get_row( "SELECT * FROM `wp_postmeta`  WHERE `meta_id` = $id" );

//will return same object like returned by get_metadata_by_mid() function

Debugging Technique:

As you mentioned, get_metadata_by_mid() isn't working on live server. I would say,

  1. Select any post id (for testing)
  2. Login to phpmyadmin
  3. Run this search query

    SELECT *  FROM `wp_postmeta` WHERE `meta_id` = ID_GOES_HERE
    //copy post_id from the results
  4. Run this query

    SELECT *  FROM `wp_terms` WHERE `term_id` = POST_ID_GOES_HERE

Try to match the process on both (local & live) server. Hopefully, it will help you identify problem.

  • Thank you so much!!! The debugging technique is very useful, the postmeta table was empty for that specific id on live server. So I added new product and there was no entry created for new product in postmeta table on live and local machine. I don't know, why. Any debugging technique for this?
    – Alena
    Dec 7, 2017 at 10:33

I'm not sure what you are trying to accomplish.

It seems you are trying to get the terms associated to a post, but the function get_metadata_by_mid gets Metadata.

If you are indeed trying to get all terms associated to a specific post, you can use the function wp_get_object_terms or wp_get_post_terms.

If you are trying to do something else, please restate your question to better explain what the intended result is ;)

  • thanks for your answer, and to be honest if I do know how get_metadata_by_mid works then I could rewrite my own query. What I need is term_id of associated post and it returns correct object when I use this code get_metadata_by_mid('post', $id); but it doesn't work on live server so I am looking for alternative way like querying db.
    – Alena
    Dec 7, 2017 at 9:02

the relationship of post and term you want is in table wp_term_relasionship. The object id equal with post_id So you can write your own query to access that

  • returns an empty array, i think it's relationship will be in some meta table but I don't know
    – Alena
    Dec 7, 2017 at 9:30
  • could you try this select t.term_id, t.name from wp_posts p inner join wp_term_relationships tr on p.ID = tr.object_id inner join wp_terms t on tr.term_taxonomy_id = t.term_id where p.ID = $id Dec 8, 2017 at 8:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.