Ich verwende die folgende Funktion, um eine zufällige eindeutige 6-stellige Zahl zu erstellen und sie in einem benutzerdefinierten Metafeld für den Beitragstyp zu speichern.
Ich bin mir nicht sicher, ob rewind_posts()
in einer while-Schleife verwendet wird. Die Idee ist, dass überprüft wird, ob die $random
Nummer bereits in einem der vorhandenen benutzerdefinierten Posts vorhanden ist. Wenn es existiert, generiert es eine neue Zufallszahl und vergleicht sie erneut mit allen benutzerdefinierten Beiträgen.
function create_random_unique_id() {
// Create random 6 digit number
$random = substr( Rand() * 900000 + 100000, 0, 6 );
// Get all Custom Posts
$args = array( 'post_type' => 'my_custom_post_type', 'posts_per_page' => -1 );
$loop = new WP_Query( $args );
// For each Custom Post, check if $random matches $cpt_id
while ( $loop->have_posts() ) {
$loop->the_post();
$cpt_id = get_post_meta ( get_the_id(), 'id', true );
// If $random matches $subscriber_id assign a different random 6 digit number
if ( $cpt_id == $random ) {
$random = substr( Rand() * 900000 + 100000, 0, 6 );
rewind_posts();
}
}
return $random;
}
Dies ist eine ziemlich teure Operation, was Sie tun, und meiner Meinung nach auch ein falscher Arbeitsablauf. Was Sie tun möchten, ist Folgendes
Holen Sie sich alle meta_value
s von dem spezifischen meta_key
, den Sie benötigen.
Sie können dann die Zufallszahl mit den Werten vergleichen, die von einem bestimmten meta_key
zurückgegeben wurden.
Hier ist eine sehr grundlegende Idee im Code: ( Dank an Chinmoy Paul von pwdtechnology.com für den Code. Code ist zum leichteren Verständnis kommentiert )
Folgendes geht in 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;
}
Um ein bestimmtes Array von Metawerten aus einem bestimmten meta_key
aus einem bestimmten post_type
und post_status
abzurufen, können Sie Folgendes versuchen
// 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;
Sie können alles in einer voll funktionsfähigen Funktion zusammenstellen.
/**
* 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;
}
Dann können Sie es wie folgt verwenden
$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;