I have a scenario where I have a meta key and value and a WordPress object name, I want to search based on the meta key and value and retrieve all details stored in db, for example if this is for a user I want the data from wp_users and wp_usermeta, same goes for other objects that have a object and an objectmeta table.

For search I have been using the query classes WP_Query, WP_User_Query, and other related classes. I get the standard attributes from there, but to get the meta attributes I still need to call another function.

I am wondering if there exists a method where this can be done, as a last resort I can always go and write inner join queries on my own.

Any leads on this are appreciated.

  • 1
    Basic question, I know, but since you didn't specifically mention this I thought it best to ask....have you tried adding global $post to your query? That lets you access all metadata associated with any specific post. Codex: codex.wordpress.org/Global_Variables A good discussion here: wordpress.stackexchange.com/questions/170358/…
    – Trisha
    Jun 22, 2022 at 9:27
  • Thanks for the lead Trisha but I believe I wont be able to use the global variable $post I am not working with a specific post, I have a meta and its value, I wanted to see if there is a way to get the post and all its data by querying based on the meta field and value, and also this is not limited to posts, I am exploring for a similar mechanism for all other WordPress objects like User, Terms, etc. Feel free to correct me if I have got anything wrong here.
    – Manas
    Jun 29, 2022 at 18:08
  • Gotcha, in that case what you use is the WP_Meta_Query, you can read how to use WP_Meta_Query here: developer.wordpress.org/reference/classes/wp_meta_query
    – Trisha
    Jul 1, 2022 at 22:30


Your Answer

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

Browse other questions tagged or ask your own question.