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();   
endwhile;
endif; 
wp_reset_postdata();
    
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?

Thanks

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

WP_query has incorrect wp_posts.post_name = ‘asc’ when I have custom post type called ‘order’

This is my code :

print_r($args)

//Output 
array(6) { ["post_type"] => string(9) "order" 
           ["date_query"] => array(2) { 
               ["before"] => string(3) "now" 
               ["compare"] => string(1) "=" 
           } 
           ["post_status"] => string(9) "validated" 
           ["posts_per_page"] => int(-1) 
           ["orderby"] => string(5) "title"
           ["order"] => string(3) "ASC" 
         } 

And this is the request :

$wp_query = new WP_Query( $args );
var_dump( $wp_query->request );
  
//Output 
"SELECT wp_posts.* 
 FROM wp_posts 
 WHERE 1=1 
 AND ( wp_posts.post_date < '2020-08-07 16:46:27' ) 
 AND wp_posts.post_name = 'asc'
 AND wp_posts.post_type = 'order'
 AND ((wp_posts.post_status = 'validated'))
 ORDER BY wp_posts.post_title ASC "

So WP_Query in the query says wp_posts.post_name = 'asc' !!!

Why ?

Go to Source
Author: J.BizMai

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

/*  CREATING COURSE PAGES FROM USER PROFILES */

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:

/* REMOVE OLD COURSES WHEN UPDATE */
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
        }
        wp_reset_postdata();
    }
}

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

Custom post type specific item design

I have a custom post type (benefits). Which as post name (bronze, silver, gold, diamond). All are listed in the same page template page-benefits.php. I want to display all on the same page but want to specific design for Diamond.
how I can achieve this while remaining in the same loop and same page template.

Go to Source
Author: Sajjad Hussain

How to get rewrited name of cusom taxonomy?

I have registered custom post taxonomy with rewrited name:

register_taxonomy('behold_gallery-albums-subject', 'behold_gallery',array(
    'hierarchical'              => true,
    'labels'                    => $labels,
    'show_ui'                   => true,
    'show_admin_column'         => true,
    'rewrite'                   => array( 'slug' => 'kategorie-galerii', 'with_front' => true ),
    'update_count_callback'     => '_update_post_term_count',
    'query_var'                 => true,
));

so name of my custom taxonomy is ‘behold_gallery-albums-subject’ but is rewrited to ‘kategorie-galerii’.

If I want to get name of this taxonomy, I can use this

$postsTaxonomy = get_sub_field('archive__post-choose-taxonomy'); // ACF
$postsTerm = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); 
$postsTerm_id = $postsTerm->term_id;
$PostsTaxName = get_taxonomy($postsTaxonomy)->labels->name;

but how can I get this rewrited name, not the ‘original’?

Thanks!

Go to Source
Author: Damian P.

images click popup form and submit images captions in email to admin?

Create Custom Post gallery..archive page display images.
customer can click Enquiry Now button.. this button popup form submit all detalies name,email,phone no,msg to send email to admin

Go to Source
Author: Ashvin Monpara

Remove Header and style.css from Specific Post template

I want to add some custom code so I’ve created a custom post file with the following content.

<?php
/*
 * Template Name: Custom App
 * Template Post Type: post, page, product
 */

 ?>
 <?php get_header(); ?>
 App Code here...

I want to remove the header element <header id="header" class="main-header"> and style.css from the custom post using function.php file.

I want to keep <?php get_header(); ?> because of yoast SEO and some analytics scripts.

I don’t want to hide the main-header using CSS display:none property

Go to Source
Author: Kumar