show label text of select option rather than its value in cmb2

I am trying to display the label text of my CMB2 select not the value on the frontend. Currently my select field displays a list of a specific custom post type called fleet. When I add $gas = get_post_meta( get_the_ID(), ‘gas__assign’, true ); echo $gas; into the front of my website it is displaying the value which in this case is the post ID. Is there away I can display the selected option instead of the value?

Custom Select

 $gas->add_field( array(
        'name'          => 'Assign',
        'desc'          => 'assign a vehicle',
        'id'            => $prefix . 'gas__assign',
        'type'          => 'select',
        'options'       => get_gas_options('fleettype'),

Select Field Selection of a CPT

function get_gas_options($a) {
$args = array(
    'post_type'         => 'fleet',
    'orderby'           => 'ID',
    'post_status'       => 'publish',
    'order'             => 'ASC',
    'posts_per_page'    => -1 // this will retrive all the post that is published 
$result = new WP_Query( $args );
$title_list[''] = "Assign a Vehicle";

if ( $result-> have_posts() ) :
while ( $result->have_posts() ) : $result->the_post();
    $title_list[get_the_ID()] = get_the_title();   
return $title_list;


Go to Source
Author: bigant841

Colliding custom permalink rules

I’m developing a reviews site in WordPress, but am having some difficulties setting up the permalink structure I have in mind.

I created two extra custom post types “reviews” and “products”.
They both have the taxonomy “categories” attached to it. All reviews have the same name as the deepest category attached to it.

Reason for this I want 3 different pages:

  • category page: list view of products within that category + a link to the detailed review for that category
  • review page: a detailed review of all products listed beneath each other.
  • product page: a detailed review of one particular product.

The permalink structure should be like this:

  • category page: /%category%/ (so just the category hierarchy)
  • review page: /%category%/review (I don’t use %postname% because a review will only be in one category hierarchy and I want to show the full category hierarchy in the url)
  • product page: /%category%/%postname%/review

Is this actually technically possible?
I’ve been fiddling a lot yet (with adding permastructs) but only get 1 or 2 to work, but they seem to collide with eachother. Is there a way to bypass this?


Go to Source
Author: Laurent

How do I check if a theme has a file single-cpt.php?

I’m creating a cpt plugin that will add ‘single-cpt.php’ in the theme directory.
The following code works but only if I put it in a functions.php (theme) folder.

// check if the file exists
$checkFile = locate_template('single-cpt.php');
if (!empty($checkFile)) {
    echo "Found!";
} else {
    echo "Not found";

How do I get the plugin check if the current theme has that single-cpt.php file ?

Go to Source
Author: Ash

After creating Custom post type by user delete old one

I have created a form where users update their profile. When a profile is created or updated it creates a CPT called course. The url is the permalink + /course/ + the title of the course


function create_course_page( $user_id = '' ) {
   $user = new WP_User($user_id);
   if ( ! $user->ID ) return '';

   // check if the user whose profile is updating has already a post
   global $wpdb;
   $course_post_exists = $wpdb->get_var( $wpdb->prepare(
   "SELECT ID FROM $wpdb->posts WHERE post_name = %s AND post_type = 'course' and post_status = 'publish'", $user->course_name
   ) );

  if ( ! in_array('instructor', $user->roles) ) return '';

  $user_info = array_map( function( $a ){ return $a[0]; }, get_user_meta( $user->ID ) );
  $title = $user_info['course_name'];
  // of course create the content as you want
  $content = 'This is the page for: ';
  $content .= $user_info['description_course'];
  $post = array(
    'post_title' => $title,
    'post_name' => $user->course_name,
    'post_content' => $content,
    'post_status' => 'publish',
    'post_type' => 'course'
  if ( $course_post_exists ) {
    $post['ID'] = $course_post_exists;
    wp_update_post( $post );
  } else {
    wp_insert_post( $post );

add_action( 'personal_options_update', 'create_course_page' );
add_action( 'edit_user_profile_update', 'create_course_page' );

The problem that I’m facing is that when someone changes the title of the course it creates a new post with this url.

So I want to delete the old post when a title change is done on the course.

NOTE: When changing title of the course it changes the URL, that is why I think it takes like a new post.

I have customised this code a bit to do the job:

add_action( 'admin_init', 'removing_older_posts' );
function removing_older_posts() {
    $args = array (
        'post_type'         => 'course',
        'author'            => get_current_user_id(),
        'orderby'           => 'date',
    $query = new WP_Query( $args );

    if ( $query->have_posts() ) { //if the current user has posts under his/her name
        $i = 0; //create post counter
        while ( $query->have_posts() ) { //loop through each post
            $query->the_post(); //get the current post
            if ($i > 0) { //if you're not on the first post
                wp_delete_post( $query->post->ID, true ); //delete the post
            $i++; //increment the post counter

But it isn’t working.

Appreciate any suggestion 🙂

Go to Source
Author: Zaesar

Randomly sort part of already sorted query

I’m not entirely sure how to word my question, but I’ll give it a go.

Basically, what I’m wanting to do is order a group of posts by a custom field. I have a True/False custom field, created using the ACF plugin, which is displayed on a certain custom post type.

Right now, I’ve set up a query to firstly display posts with the True/False statement set to true, and then to display the posts with the statement set to false. This is working well.

What I want to do, however, is take the posts with the true statement and randomly change their order. All while keeping the current order or True first and then False in place.

Here is my current query.

                $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
                // the query
                $wpb_all_query = new WP_Query(array('post_type'=>'businesses', 'post_status'=>'publish', 'category_name'=>basename(get_permalink()), 'meta_key'=>'featured', 'orderby'=>'meta_value','order'=>'DESC', 'posts_per_page'=>20, 'paged' => $paged)); ?>

                    <?php if ( $wpb_all_query->have_posts() ) : ?>
                    <!-- the loop -->
                    <?php while ( $wpb_all_query->have_posts() ) : $wpb_all_query->the_post(); ?>

Go to Source
Author: Timothy Davis