Output custom post type name and post id in a string

Hey everyone,

I would like to have a unique reference for my posts. For this I decided to output 2 letters from the custom post type name and the unique post ID which is generated automatically by wordpress.

So for example if my post type is called “os_fashion” and the post ID is 23456 - I would like to output in a string fa23456 which is the 3rd and 4th character of the custom post type name combined with the post ID.

I require some help with coding this as my php skills are quite limited.

thanks in advance for your help guys
Andy

Hello, I would use something like this.
I will explain a little but of what is going on within the function.
The first line in the function which returns $string simply uses str_replace to remove the underscore, if you want to leave the possibility of including the underscore this line would then simply look like this:


$string = str_shuffle($post_type);

The second line just grabs the characters of the shuffled string, the length of which is determined by the size of the limit parameter. The next line appends the digits to the create the desired string. Hope this helps.


<?

$post_type = 'os_fashion';
$post_id = 23456;

function generate_random_output($post_type, $post_id, $limit = 2)
{
    $string = str_shuffle(str_replace('_', '', $post_type));
    $output = substr($string, 1, $limit);
    $output .= $post_id;
    return $output;
}
echo generate_random_output($post_type, $post_id);

?>

Keeping things simple - I’m assuming os_ prefix is always 3 characters (including the underline) how about:


$post_ID = 'os_fashion';
$post_type = 23456;    
$prefix = substr ( $post_ID , 3, 2 );
echo $post_ID.$post_type;

Output:

fa23456

thanks guys this works!

Just noticed, I put the wrong variable in the echo - should be:


$post_ID = 'os_fashion';
$post_type = 23456;    
$prefix = substr ( $post_ID , 3, 2 );
echo $prefix.$post_type;

That what happens when you don’t test it first. Guess you noticed this anyway.