wp_set_object_terms() is not replacing term, but creating a new one

In both the cases issue is not the other things, but the main value what you are sending to the second parameter, in your case $flightCategory: $flightCategory = array( 25 ); var_dump( $flightCategory ); wp_set_object_terms( $post_id, $flightCategory, ‘flight_categories’ ); But on the later version somehow, or literally you are actually passing something like below: $flightCategory … Read more

How to add a button to custom post type’s posts-page

You can add button via add_meta_box function. function add_your_meta_box(){ add_meta_box(‘your-metabox-id’, ‘Title’, ‘function_of_metabox’, ‘custom_post_type’, ‘side’, ‘high’);} add_action(‘add_meta_boxes’, ‘add_your_meta_box’); function function_of_metabox() {?> <input type=”submit” class=”button button-primary button-large” value=”Add New” id=”add-new”/> <?php } if you add to multiple post type, you should use foreach loop. function add_your_meta_box(){ $types = array(“post”,”page”,”custom_post_type”); foreach($types as $type){ add_meta_box(‘your-metabox-id’, ‘Title’, ‘function_of_metabox’, $type, ‘side’, … Read more

Create a dropdown with Custom Post Types as option in admin

Here is the code I’m using in a project I’m working on. function generate_post_select($select_id, $post_type, $selected = 0) { $post_type_object = get_post_type_object($post_type); $label = $post_type_object->label; $posts = get_posts(array(‘post_type’=> $post_type, ‘post_status’=> ‘publish’, ‘suppress_filters’ => false, ‘posts_per_page’=>-1)); echo ‘<select name=”‘. $select_id .'” id=”‘.$select_id.'”>’; echo ‘<option value = “” >All ‘.$label.’ </option>’; foreach ($posts as $post) { echo … Read more

Search that will look in custom field, post title and post content

First, don’t use query_posts. Second, you can pass an s parameter to get most of the way there. $program_search=”test”; $args = array( ‘post_type’ => ‘program’, ‘s’ => $program_search, ‘meta_query’ => array( array( ‘key’ => ‘keywords’, ‘value’ => $program_search, ‘compare’ => ‘LIKE’ ), ) ); $t = new WP_Query($args); var_dump($t->request); That s parameter kicks the ordinary … Read more

query multiple taxonomies

First of all, get all term slugs from the custom taxonomy space by current post ID. $space_terms = wp_get_post_terms( $post->ID, ‘space’ ); if( $space_terms ) { $space_terms = array(); foreach( $space_terms as $term ) { $space_terms[] = $term->slug; } } You should specify the logical relationship between each inner taxonomy array when there is more … Read more

Using standard search function with custom post type

Ok, so I did a little more digging around and it turns out to be pretty easy. I just needed to add a hidden input into the search form. Posting this here for anyone else looking for the answer: <form class=”search” action=”<?php echo home_url( “https://wordpress.stackexchange.com/” ); ?>”> <input type=”search” name=”s” placeholder=”Search&hellip;”> <input type=”submit” value=”Search”> <input … Read more

how to filter by last name for custom post

Order by the last word in the post title To order by the speaker’s last name, you can use the following setup (PHP 5.4+): // args $args = [ ‘posts_per_page’ => 10, ‘post_type’ => ‘speaker’, ‘meta_key’ => ‘speaker-front-page’, ‘meta_value’ => ‘1’, ‘orderby’ => ‘wpse_last_word’, //<– Our custom ordering! ‘order’ => ‘ASC’ ]; // query $the_query … Read more