Skip to the content
RND Experts
  • Home
  • Insights
    About Us

    Elevate your online presence with our dedicated web development team. We blend innovation and precision, creating bespoke solutions that redefine digital landscapes.

    Blogs

    Explore the latest in web development through our insightful blogs. From coding tips to industry trends, we share valuable knowledge to enhance your skills.

    Case Studies

    Whether you're a potential client looking for proof of our capabilities or a current client interested in exploring similar use cases, our Case Studies section is a valuable resource.

    Our Products

    Revolutionize your digital presence with our cutting-edge web development product. Seamlessly combining functionality and aesthetics, it empowers your online endeavors.

    Careers

    Embark on a rewarding web development career with us. Join a dynamic team where innovation meets expertise, fostering professional growth.

    Hire Experts

    Elevate your projects with our team of seasoned experts. We bring a wealth of experience and proficiency to every task.

  • Services
    Development
      Laravel Laravel Development
      CakePHP Cakephp / CodeIgniter Development
      Yii Yii Development
      WordPress WordPress Development
      WooCommerce eCommerce Development
      NodeJS NodeJs Development
    Design
      HTML CSS Design HTML / CSS Design
      Photoshop Design Photoshop Design
      Figma Design Figma Design
      Canva Design Canva Design
      Email Template Design Email Template Design
    Marketing
      SEO Search Engine Optimization (SEO)
      SMM Social Media Marketing (SMM)
      SMO Social Media Optimization (SMO)
      ORM Online Reputation Management
    Others
      2D Platform 2D Platform
      Website Security Website Securities
  • Portfolio
  • Testimonials
  • Contact Us
× Certified Image
Categories
Technology

Finding Post Based on Post Meta Value in WP Query

  • Post author By Admin
  • Post date April 16, 2021
  • No Comments on Finding Post Based on Post Meta Value in WP Query
wordpress

Recently I was working on a Custom Post Type plugin under the banner of RND Experts, where the user will enter the name of the city and the field will get saved in the wp_post_meta table. The client has requested to create a shortcode where we can filter Posts on the basis of Single city or Multiple comma-separated cities.

Here is the code I’ve written to accomplish the requirement.

$atts = shortcode_atts( array( 
'city' => '',
),$atts 
);
$args = array(
'post_type'  => 'wp_property', 
'posts_per_page'  => -1
);
if( $atts['city'] != '' ) {
$cities = explode( ',', $atts['city'] );
$meta_query = array();
if( !empty($cities) ) {
foreach ($cities as $key => $city) {
$meta_query[] = array(
'key'  => 'cptwh_property_city',
'value'  => strtolower(trim($city)),
'compare'  => 'LIKE'
);
}
} 
$args['meta_query']['relation'] = 'OR';
$args['meta_query'] = $meta_query;
} 
$properties = new WP_Query( $args ); 

1. Here are the steps to do this.


2. Extract the attribute values. Like, I’ve used city attribute.


3. Now you have to check if users have assigned any value to the City attribute. If there is no value we will not process the Meta Query.


4. In case we have found any value in the City. Let, ‘s find the number of cities entered by using the explode function.


5. Now parse the exploded string using the for each loop and assign values to a newly created array variable meta_query.


6. Now, after the Array element use a ‘relation’ element for the comparison method. The default value of ‘relation’ is ‘AND’. If in case you do not provide any value it will find the records which fall under the values. I’ve assigned value OR to this so that I can fetch all the Posts that have City 1 and City 2 in its meta table.

  • Tags RND Experts, wordpress custom search query, wordpress custom sql, wordpress wp query, wp post meta table, WP Query
  • Trusted By
  • clutch
  • upwork
  • behance
  • facebook

RND EXPERTS INDIA

We're always looking for new projects to work on, if you have some please write us!

D-151, Ground Floor, Phase-8, Industrial Area, Mohali-160055, Punjab.
+91-946-4554-090 +91-771-7324-545
webrndexperts@gmail.com info@rndexperts.com
webrndexperts

RND EXPERTS USA

We're always looking for new projects to work on, if you have some please write us!

43 plum road suite 101 Nonsey New York 10952-1525
+91-946-4554-090 +91-771-7324-545
webrndexperts@gmail.com info@rndexperts.com
webrndexperts

We have world class Technology Experts @ RND Experts Pvt. Ltd.

Services

Web Development

  • Laravel Development
  • Cakephp/CodeIgniter Development
  • Yii Development
  • WordPress Development
  • eCommerce Development
  • NodeJs Development

AI/ML

  • AI Software Development Services
  • AI Development Services
  • ChatGPT Integration Services
  • AI Integration Services
Solutions
  • Ride-Sharing & Taxi Services
  • Food Delivery Services
  • Grocery Delivery Services
  • Transportation & Logistics
  • Custom LMS Development
  • Custom CRM Development
Industries
  • Healthcare
  • E-Commerce & Retail
  • Education
  • Finance
  • Food & Restaurant
  • Real Estate
  • Travel
Quick Links
  • Home
  • About Us
  • Services
  • Portfolio
  • Blog
  • Testimonials
  • Contact
Resources
  • Careers
  • Case-studies
  • Blogs
Write us on: webrndexperts@gmail.com
Business queries: +91 9464554090
HR: hr@rndexperts.com
HR: +917717324545

Privacy Policy | Copyright © 2014-2026 RND Experts Pvt. Ltd.