WordPress: Get checked/selected taxonomy label - php

I am trying to figure out how to get taxonomy(checkboxes) label from a custom post type that is checked/selected to show on the single custom post. The code below is outputting all the taxonomies not just the checked ones.
function get_terms_chekboxes($taxonomies, $args) {
$terms = get_terms($taxonomies, $args);
foreach($terms as $term){
$output .= $term->name ;
}
return $output;
}
echo get_terms_chekboxes('genre', $args = array('post_type' => 'movie','hide_empty'=>false));
How to get checked taxonomy labels.
Thanks.
Reference

Please try this function for retrieving the term_names of single custom posts assigned to it.
$term_array = array();
$term_list = wp_get_post_terms($post->ID, 'genre', array("fields" => "all"));
foreach($term_list as $term_single) {
$term_array[] = $term_single->name ; //do something here
}
echo implode(", ",$term_array);
where $post->ID is the ID of the single custom post & 'genre' is your taxonomy slug.
I hope, this may be helpful to you.

Related

Taxonomy ID by Post ID in custom endpoint

I making custom endpoint for my custom post type. The one thing is the taxonomy ID. I need to get taxonomy ID by post ID. get_terms( 'gallery_tax', $post->ID ) gives me array with all taxonomy object
function wl_posts() {
$args = [
'numberposts' => 9999,
'post_type' => 'gallery',
];
$posts = get_posts($args);
$data = [];
$i = 0;
foreach ($posts as $post) {
$data[$i]['id'] = $post->ID;
$data[$i]['fimg_url'] = get_the_post_thumbnail_url($post->ID, 'large');
$data[$i]['proizvoditel'] = get_field('proizvoditel', $post->ID);
$data[$i]['tip'] = get_field('tip', $post->ID);
$data[$i]['razmer'] = get_field('razmer', $post->ID);
$data[$i]['forma'] = get_field('forma', $post->ID);
$data[$i]['rost'] = get_field('rost', $post->ID);
$data[$i]['ves'] = get_field('ves', $post->ID);
$data[$i]['ohvat'] = get_field('ohvat', $post->ID);
$data[$i]['vozrast'] = get_field('vozrast', $post->ID);
$data[$i]['galereya'] = get_field('galereya', $post->ID);
$data[$i]['taxonomy'] = get_terms( 'gallery_tax', $post->ID );
$i++;
}
return $data;
}
add_action('rest_api_init', function() {
register_rest_route('wl/v1', 'gallery', [
'methods' => 'GET',
'callback' => 'wl_posts',
]);
});
Using get_terms('gallery_tax') will give you all the terms in a taxonomy.
https://developer.wordpress.org/reference/functions/get_terms
You get all the existing terms in your taxonomy. So this is why you get the result.
Using get_the_terms($post->ID, 'gallery_tax') will give you all taxonomy terms attached to the post.
https://developer.wordpress.org/reference/functions/get_the_terms/
You get all the terms that have been assigned to your post.
If you want to display the name of the taxonomy itself and not display the terms associated with the post, you can first get all the names of the taxonomies outside of your post loop and then get the taxonomy name inside of your foreach:
...
$data = [];
$i = 0;
$taxnames = get_taxonomies('','names');
foreach ($posts as $post) {
...
$data[$i]['taxonomy'] = wp_get_post_terms($post->ID, $taxnames, array("fields" => "names"));
$i++;
}
...
https://developer.wordpress.org/reference/functions/get_taxonomies/

How can I get terms (post_tag) of custom taxonomy item? Wordpress

I created custom post type 'photo' and custom taxonomy 'catphoto'.
The type 'photo' supports 'catphoto' and 'post_tag' as taxonomies.
Now I should make one filter for a client.
I need to show on taxonomy-catphoto.php page all post_tags, which belong to the current 'catphoto' item.
For example, I have a post of custom post type 'photo'. The name of this post is 'Plane'. This post belongs to '1961-1981' catphoto item. Also it has post tags like 'space', 'planes', 'stars', 'war'.
Also, for example, I have a post of 'photo' which name is 'Soldier'. It belongs to '1941-1961' catphoto item and has 'WW2', 'USA', 'USSR' like post tags.
And now when I select 1941-1961 catphoto item I should get a list with:
WW2
USA
USSR
I try like this:
if (substr($_SERVER['REQUEST_URI'],1,8) == 'catphoto'){
$cur_terms = get_terms('post_tag');
if ($cur_terms) {
foreach( $cur_terms as $cur_term ){
echo $cur_term->name.'<br/>';
}
}
}
Now I get all post tags of all catphoto items. How can I fix to restrict for definite catphoto item. For example, '1941-1961' ?
I have solved this via $_GET query. So easy...
For beginning, I had decided to create own taxonomy type (like post_tags). Its name is 'mark'.
and after that the snippet is:
if (substr($_SERVER['REQUEST_URI'],1,8) == 'catphoto'){
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
$query = new WP_Query( array('post_type' => 'photo', 'catphoto' => $term->name));
echo '<ul>';
$uniqueTerms = array();
$uniqueSlugs = array();
$uniquePar = $term->slug;
while ( $query->have_posts() ) {
$query->the_post();
$terms = wp_get_post_terms(get_the_ID(), 'mark', array("fields" => "all"));
foreach ($terms as $term1)
{
if(!in_array($term1->name, $uniqueTerms)) {
$uniqueTerms[] = $term1->name;
$uniqueSlugs[] = $term1->slug;
}
}
}
for ($var = 0; $var < count($uniqueTerms); $var++)
echo '<li>'.$uniqueTerms[$var].'</li>';
echo '</ul>';
}
So ugly code here and there but it works.
and now in taxonomy-catphoto.php I fill in:
$mark = $_GET['mark'];
and after that a little change inside WP_Query query
$query = new WP_Query( array( 'post_type' => 'photo', 'posts_per_page'=>56, 'paged'=> $paged, 'catphoto' => $term->name, 'mark' =>$mark) );
Best regards, Igor

Wordpress wp_update_post not updating post_name

I have a requirement in WordPress, where , if the taxonomy term string is equal to post title, the update the slug (since the permalink rules fail in this case).
My code is :
add_action('save_post', 'change_default_slug', 10,2);
function change_default_slug($post_id, $post) {
error_log($post_id);
error_log($post->post_title);
error_log($post->post_name);
$taxonomies=get_taxonomies('','names');
$terms = wp_get_post_terms($post->ID, $taxonomies, array("fields" => "names"));
$terms = array_map('strtolower', $terms);
error_log('terms :' . json_encode($terms));
$title = strtolower($post->post_title);
error_log('title : ' . $title);
if(in_array($title, $terms)) {
error_log('yessss');
$args = array (
'ID' => $post->ID,
'post_name' => $post->post_name . "-post"
);
$result = wp_update_post($update_args);
error_log('result');
error_log(json_encode($result));
} else {
error_log('noooooooo');
}
}
On required post I am getting logs : Yesss result 0.
The slug is not updating.
Please help on the same. I have tried literally all solutions available for this issue. It has to be done via functions.php
I was finally able to solve it using : wp_insert_post()
$taxonomies=get_taxonomies('','names');
$terms = wp_get_post_terms($post->ID, $taxonomies, array("fields" => "all"));
foreach($terms as $term) {
if($term->taxonomy == 'category'){
$categories[] = $term->term_id;
} else if($term->taxonomy == 'post_tag'){
$tags[] = $term->term_id;
}
}
.
.
.
//detach the hook to avoid infinite looping of the hook on post insert
remove_action('save_post', 'change_default_slug', 10,2);
//insert post
$result = wp_insert_post($post, true);
//attach post tags to the current post (since not by default attached)
wp_set_post_terms($post_id,$tags,'post_tag');
//attach post categories to the current post (since not by default attached)
wp_set_post_terms($post_id,$categories,'category');
//re-activate the hook
add_action('save_post', 'change_default_slug', 10,2);

How to get only child terms of current post type?

Having a custom post type 'pubs' with custom taxonomy 'types' in which admin enter parent terms and their child terms.
Using this code to get all the terms of current post type:
$object_terms = wp_get_object_terms($post->ID, 'types', array('fields' => 'all'));
if ($object_terms) {
echo '' . '' . '' ;
$res = '';
foreach ($object_terms as $term) {
$res .= $term->name . ',';
}
echo rtrim($res,' ,').'' . '';
}
this code displays both parent & child terms.
Is there any way to exclude parent terms from the result? I need the code to display only child terms related to the current post.
Untested, but I think if you put the following inside your foreach loop at the very top, you'll get only the children:
if ($term->parent == 0) continue;
Just for someone still looking:
Solution is for when you have multiple level of hierarchy and you want just last level.
$term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
foreach ($term_array as $term_id){
$children=get_term_children($term_id, $taxonomy);
if(empty($children)){
$exclude=$term_id;
}
}

Count published custom posts in Wordpress

I have the following piece of code in a WordPress site which has custom posts.
This appears in a functions.php file.
It was a purchased template and I need to count the just the PUBLISHED custom posts and i added this in the code below :
'.'('.$option->count.')'.'
It is working just fine at the moment, but it's counting trash as well.
Please could somebody help me, thank you very much.
function dox_get_list_terms( $taxonomy = 'category', $term_id, $number, $orderby = 'name', $order = 'ASC', $hide = '0' ) {
$terms = array();
$terms = explode(',', $term_id);
$count = count( $terms );
$output = '';
foreach( $terms as $term ) {
if ($term >= 0) {
$options = get_terms( $taxonomy, 'number='.$number.'child_of='.$term.'&parent='.$term.'&hide_empty='.$hide.'&hierarchical=1&depth=1&orderby='.$orderby.'&order='.$order );
if (! is_wp_error($options) ) {
foreach ($options as $option) {
$output .= '<li>
<a href="'.get_term_link($option->slug, $taxonomy).'">
'.$option->name.'
</a>
'.'('.$option->count.')'.'
</li>';
}
}
}
}
return $output;
}
If you set hide_empty=1 in get_terms() function, Then you`ll only get terms who are assigned to any published post or custom posts.
Terms with 0 count will be ignored.

Categories