Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8374545
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:56:48+00:00 2026-06-09T14:56:48+00:00

This is a wordpress database and I have 2 tables of interest: posts and

  • 0

This is a wordpress database and I have 2 tables of interest: posts and postmeta. What I want to do is to get $term(searched by users) and use this variable on the SQL search.

The tables I want to search if there is a match are meta_value(if meta_key is albumYear), post_content and post_title.

The problem is that meta_value is in a different table than the others, but its table postmeta has post_id, which is the same id that posts.ID. This makes possible to know that some meta value belongs to some post.

I tried the following code and I hope I made it clear what I need so I can get help from people here. I really need help on this and I couldn’t find the answer on google or here. Thanks in advance.

$query_sql = "
    SELECT post_id, meta_key, meta_value FROM $wpdb->postmeta
    ,
     (SELECT *
     FROM $wpdb->posts
     WHERE NOT wpdb->posts.post_status = 'inherit'
     AND (wpdb->posts.post_type = 'page' OR wpdb->posts.post_type = 'post')
     AND (wpdb->posts.post_content LIKE '%".$term."%' OR wpdb->posts.post_name LIKE '%".$term."%' OR $wpdb->postmeta.meta_value LIKE '%".$term."%')
     ORDER BY wpdb->posts.post_title ASC) x
     WHERE $wpdb->postmeta.post_id = $wpdb->posts.ID AND $wpdb->postmeta.meta_key = 'albumYear'
";

$query_result = $wpdb->get_results($query_sql, OBJECT);

Sample of the SQL structure:

1.wp_posts
1.1 ID, post_type, post_content, post_status, post_name, post_title etc

2.wp_postmeta
2.1 post_id, meta_key, meta_value

So, I need to search matches for $term in meta_value(when meta_key is X), post_content, post_name etc. But also I need to identify the Post im searching and return all of its content.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T14:56:49+00:00Added an answer on June 9, 2026 at 2:56 pm

    All you need is just a join of two tables.

    Sorry, don’t have WP available atm, so can’t check it, but should like something like that:

    $query_sql = "
      SELECT 
        post_id, meta_key, meta_value,
        $wpdb->posts.*
      FROM $wpdb->posts
      LEFT JOIN $wpdb->postmeta ON $wpdb->posts.ID = $wpdb->postmeta.post_id
      WHERE NOT wpdb->posts.post_status = 'inherit'
        AND (wpdb->posts.post_type = 'page' OR wpdb->posts.post_type = 'post')
        AND (wpdb->posts.post_content LIKE '%".$term."%' OR 
             wpdb->posts.post_name LIKE '%".$term."%' OR 
             $wpdb->postmeta.meta_value LIKE '%".$term."%')
        AND $wpdb->postmeta.meta_key = 'albumYear'
      ORDER BY wpdb->posts.post_title ASC
    ";
    

    Add your checks and query fields list if you need it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 2 tables (Wordpress Database), one which stores posts and the other post
I'm trying to update tables in my wordpress mu database. I want to update
I have this strings saved in my mysql database in format like: /uploads/attachments/18/105/WordPress_3_Cookbook.pdf Now,
I have a table like this (using wordpress) +---------+----------+------------+ | meta_id | meta_key |
I have a table in the same database as my wordpress install. What is
I have basically MySQL dump from a WordPress database with some data. The whole
I have a (Wordpress) blog and some of my older posts have a character
I have a custom table in my WordPress database named post_votes used for voting
I have a wordpress website, and I want to create a simple web commerce.
I have WordPress instances with each in its own database. For an update I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.