Skip to content

Instantly share code, notes, and snippets.

@utkrishta
Created February 18, 2022 03:57
Show Gist options
  • Save utkrishta/01e98eb2f118f1cb17366468cbccf19b to your computer and use it in GitHub Desktop.
Save utkrishta/01e98eb2f118f1cb17366468cbccf19b to your computer and use it in GitHub Desktop.
<?php
/*
* We are using rank_math_locations custom post type. (or any other)
* Meta Box to create custom fields which saves in its own database table
* We have location metabox custom field saved in table "mbox_locations" and
* service type custom field saved in table "mbox_services"
*
* mbox_locations contains a sub-field(toggle) called capital-cities
* mbox_services contains a sub-field(radio) called service-type
*/
//get all rank-math-locations where "capital cities" is toggled ON and service-type is "plumbing"
global $wpdb;
$locations_sql = "SELECT mbox_locations.ID " .
"FROM mbox_services, mbox_locations " .
"WHERE " .
"mbox_locations.capital_cities = 1 AND " .
"mbox_services.service_type LIKE '%plumbing%' AND " .
"mbox_locations.ID = mbox_services.ID;";
$locations_ids = $wpdb->get_col( $locations_sql );
$locations_args = array(
'post__in' => $locations_ids,
'post_type' => 'rank_math_locations',
);
$locations_query = new WP_Query( $locations_args );
if ( $locations_query->have_posts() ) {
while ( $locations_query->have_posts() ) {
$locations_query->the_post();
the_title();
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment