wp_set_object_terms NOT working on CPT, but wp_set_post_terms does

After much trial and error, I have found a solution that will allow wp_set_object_terms to be used. Despite already checking if my strings were integers, apparently I needed to convert the array explicitly as well. So I changed this: $customtax = $customtaxarray; to this: $customtax = array_map(‘intval’,$customtaxarray); and suddenly wp_set_object_terms now works. That does not … Read more

Is it possible to use array_walk() to append terms to an array of posts?

I guess you have a problem with variable scopes in here. array_walk($terms, function(&$term, $term_index) { // die(print_r($term)); if (isset($term->slug)) { array_push($post->terms, $term->slug); } }); In this part of your code, you use anonymous function and inside of it you use $post variable. But it is a different variable than the one from few lines back … Read more

one post per term taxonomy

Yeah sure. <?php $included_post_ids =array(); $args = array( ‘post_type’ => ‘APP_POST_TYPE’, ‘post_status’ => ‘publish’, ‘posts_per_page’ => 1, ‘tax_query’ => array( array( ‘taxonomy’ => ‘coupon_category’, // Taxonomy ‘field’ => ‘slug’, ‘terms’ => $tax[0]->slug, // Your category slug ), ), ); $custom_posts = get_posts( $args ); if( !empty( $custom_posts ) ){ foreach ($custom_posts as $key => $postObj) … Read more

Parent category / child category posts

OK solved! I have a post called “Cardio” with a child category name “Cardio”. I created a category-cardio.php (cardio is the child category) but the page doesn’t display the post “cardio” same name as the category. <?php /** * Cardio category template * * @package clf */ get_header(member); ?> <div id=”primary” class=”content-area”> <main id=”main” class=”site-main”> … Read more

Getting the post terms ‘wp_get_post_terms’ per post when within the functions.php file

(Revised answer) So I’m assuming that there are actually posts in the category (ID: 31), and based on the wp_get_post_terms() reference: $post_id (int) (Optional) The Post ID. Does not default to the ID of the global $post. Default 0. It’s likely that you’re not passing the proper post ID to the wp_get_post_terms() function: wp_get_post_terms( $post->ID, … Read more