这是一个相当昂贵的操作,你在做什么,我也有错误的工作流程。你想做的是
获取所有meta_value
来自特定meta_key
你需要。
然后,您可以将随机数与特定meta_key
这里有一个关于代码的非常基本的想法:(该代码归功于pwdtechnology.com的Chinmoy Paul。为了便于理解,对代码进行了注释)
以下内容涉及functions.php
/**
* Description: Getting all the values associated with a specific custom post meta key, across all posts
* Author: Chinmoy Paul
* Author URL: http://pwdtechnology.com
*
* @param string $key Post Meta Key.
*
* @param string $type Post Type. Default is post. You can pass custom post type here.
*
* @param string $status Post Status like Publish, draft, future etc. default is publish
*
* @return array
*/
function get_unique_post_meta_values( $key = \'\', $type = \'post\', $status = \'publish\' )
{
global $wpdb;
if( empty( $key ) )
return;
$res = $wpdb->get_col(
$wpdb->prepare(
"SELECT DISTINCT pm.meta_value
FROM {$wpdb->postmeta} pm
LEFT JOIN {$wpdb->posts} p
ON p.ID = pm.post_id
WHERE pm.meta_key = \'%s\'
AND p.post_status = \'%s\'
AND p.post_type = \'%s\'",
$key,
$status,
$type
)
);
return $res;
}
现在,从给定的
meta_key
从特定
post_type
和
post_status
, 您可以尝试以下方法
// Get ids to exclude
$ids_to_exclude = get_unique_post_meta_values(
\'id\', // This is our meta_key to get values from
\'MY_POST_TYPE\', // This is the name of your custom post type
\'POST_STATUS\' // Any other post status except publish as publish is default
);
/**
* Generate a unique id
* Thanks to Gautam3164 for the code
* @see http://stackoverflow.com/a/17109530/1908141
*/
do {
// This is taken as is from your code in question
$random = substr( rand() * 900000 + 100000, 0, 6 );
}
while( in_array( $random, $ids_to_exclude ) );
echo $random;
编辑您可以将所有内容整合到一个功能齐全的功能中。
/**
* Description: Get a random unique 6 number id for any given meta_key
*
* @param string $key Post Meta Key.
*
* @param string $type Post Type. Default is post. You can pass custom post type here.
*
* @param string $status Post Status like Publish, draft, future etc. default is publish
*
* @return array
*/
function get_unique_random_value(
$key = \'\',
$type = \'post\',
$status = \'publish\'
) {
global $wpdb;
// Check if we have a meta_key before continuing, if not, return false
if( empty( $key ) )
return false;
$res = $wpdb->get_col(
$wpdb->prepare(
"SELECT DISTINCT pm.meta_value
FROM {$wpdb->postmeta} pm
LEFT JOIN {$wpdb->posts} p
ON p.ID = pm.post_id
WHERE pm.meta_key = \'%s\'
AND p.post_status = \'%s\'
AND p.post_type = \'%s\'",
$key,
$status,
$type
)
);
// Check if we actually have an array of values, if not, return false
if ( !$res )
return false;
/**
* Generate a unique id
* Thanks to Gautam3164 for the code
* @see http://stackoverflow.com/a/17109530/1908141
*/
do {
// This is taken as is from your code in question
$random = substr( rand() * 900000 + 100000, 0, 6 );
}
while( in_array( $random, $res ) );
return $random;
}
然后您可以按如下方式使用它
$random_id = get_unique_random_value (
\'id\', // This is our meta_key to get values from
\'MY_POST_TYPE\', // This is the name of your custom post type
\'POST_STATUS\' // Any other post status except publish as publish is default
);
echo $random_id;