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 7666677
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:53:39+00:00 2026-05-31T14:53:39+00:00

Ive written a query that searches for all posts that have X as a

  • 0

Ive written a query that searches for all posts that have X as a meta/custom field value.

    // PSV National Query
        if ($_POST['vehicleType'] == 'psv' && $_POST['coverageRegion'] == 'national' ) {        

            $customkey = 'vehicleType'; 
            $customvalue = $_POST['vehicleType']; 

            $customkey1 = 'coverageRegion'; 
            $customvalue1 = $_POST['coverageRegion']; 

            $customkey2 = 'locationType'; 
            $customvalue2 = $_POST['locationType']; 

            global $wpdb;
            $my_posts = $wpdb->get_results("
                SELECT $wpdb->posts.* 
                FROM $wpdb->posts, $wpdb->postmeta, $wpdb->postmeta AS mt1
                WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id 

                AND $wpdb->postmeta.meta_key = '$customkey'   
                AND $wpdb->postmeta.meta_value = '$customvalue'
                AND mt1.meta_key = '$customkey1'
                AND mt1.meta_value = '$customvalue1'
                AND mt2.meta_key = '$customkey2'
                AND mt2.meta_value = '$customvalue2'

                AND $wpdb->posts.post_status = 'publish' 
                AND $wpdb->posts.post_type = 'post'
                ORDER BY $wpdb->posts.post_date DESC
            ");

            $args = array(
            'meta_query' => array(
            array(
                'key' => $customkey,
                'value' => $customvalue,
                'compare' => '='
            ),
            array(
                'key' => $customkey1,
                'value' => $customvalue1,
                'compare' => '='
            ),
            array(
                'key' => $customkey2,
                'value' => $customvalue2,
                'compare' => '='
            )
            )
            );
            $query = new WP_Query( $args );

            foreach ($query as $post) :
            setup_postdata($post);

            echo '<div><a href="';
            the_permalink();
            echo '"></div>';
            the_title();

            endforeach; 

        }

Now for my post I have 1 value for each meta key and this works fine, I want to however have multiple values.

For example…

“gas, electricity, water”

When I add multiple values however the query returns null, I presumer its because im saying if…

postmeta.meta_value = '$customvalue'

Can anybody give me advice on where im going wrong?

  • 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-05-31T14:53:40+00:00Added an answer on May 31, 2026 at 2:53 pm

    Why don’t you use the built in WP QUERY, it supports meta field array operators.

    for example:

    $query = new WP_Query( array( 
                              'meta_key'     => 'color', 
                              'meta_value'   => 'blue', 
                              'meta_compare' => '!='  
                           ));
    

    Ref: http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters

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

Sidebar

Related Questions

I have a photoshop plugin for a file format Ive written in c++ that
I have a table that contains some meta data in an XML field. For
I have a big fat query that's written dynamically to integrate some data. Basically
I have written an application that allows a user to define a query, run
I'm learning SQL and I've written a query that returns all owners of Fords.
I have a query that's written dynamically (OO PHP via Joomla) to insert some
I've written an SQL query that produces a report of some stats for each
I've written an SQL query that tells me the names of the previous week's
I've written a query that works good if used into MySql Workbench, but it
I've written a query in SQL that selects some columns from a table.My problem

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.