我将数据保存在用户_meta->;areapref字段。它基本上是一个由逗号分隔的post ID组成的字符串(例如2、37、200、300等)
我需要检查用户在此字段中是否有特定ID。如果是这样。。。做些事情。
我可以使用LIKE检查areapref中是否有ID。但我认为这有一个问题。如果一个位置ID=10,另一个位置ID=100,则两者都将与1或10匹配?还是我误会了。我担心CONTAIN也会有同样的问题。
我需要知道如何获取在areapref字符串中具有“精确匹配”的用户列表?所以搜索ID 2,只会返回2,而不是22,或20,200等。我花了几个小时浏览stackexchange/溢出/wp codex,但找不到答案。。。虽然我很笨!
这里有足够的代码,我希望显示我目前正在做什么。。。非常感谢您的任何想法。
function ds_notify_new_event( $post_ID ){
$url = get_permalink( $post_ID );
$event = get_the_title( $post_ID );
$localeID = get_post_field( \'location_id\', $post_ID);
global $wpdb;
$locale = $wpdb->get_var( $wpdb->prepare(
" SELECT post_id FROM {$wpdb->prefix}em_locations WHERE ID = %d ",
$localeID ) );
$args = array(
\'role\' => \'subscriber\',
\'meta_query\' => array(
array( \'key\' => \'areapref\', \'value\' => $locale, \'compare\' => \'LIKE\' ),
// just checks usr is ok with email
array( \'key\' => \'notify\', \'value\' => \'yes\', \'compare\' => \'=\' )
),
\'fields\' => array( \'display_name\', \'user_email\' )
);
// retrieve users to notify about the new post
$users = get_users( $args );
// do stuff with users