您可以使用WP Query
具有meta_query
参数如下。
$args = array (
\'post_type\' => array( \'post\' ), // YOUR POST TYPE
\'meta_query\' => array(
array(
\'key\' => \'country\',
\'value\' => $your_country, // THE COUNTRY TO SEARCH
\'compare\' => \'LIKE\', // TO SEARCH THIS COUNTRY IN YOUR COMMA SEPERATED STRING
\'type\' => \'CHAR\',
),
),
);
// The Query
$query = new WP_Query( $args );
我已经测试了这个查询。工作完美。希望这对你也有用。
Edit:
对于多个国家,您可以使用以下方法。
$countries_to_search = \'usa,africa,poland\'; // YOUR COMMA SEPARATED STRING
$countries = explode( \',\', $countries_to_search ); // CONVERT INTO ARRAY OF COUNTRIES
$meta_array = array();
$meta_array[\'relation\'] = \'AND\'; // DECLARE RELATION --> YOU CAN USE \'OR\' AS WELL
foreach($countries as $country){ // CREATE QUERY FOR EACH COUNTRY IN ARRAY
$meta_array[] = array(
\'key\' => \'country\',
\'value\' => $country, // THE COUNTRY TO SEARCH
\'compare\' => \'LIKE\', // TO SEARCH THIS COUNTRY IN YOUR COMMA SEPERATED STRING
\'type\' => \'CHAR\',
);
}
$args = array (
\'post_type\' => array( \'post\' ), // YOUR POST TYPE
\'meta_query\' => $meta_array, // ARRAY CONTAINING META QUERY
);
// The Query
$query = new WP_Query( $args );