Taxonomy ID by Post ID in custom endpoint - php

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/

Related

Add Custom Endpoint in WordPress Rest Api

I want to add custom endpoint in WordPress Rest API.I am able to fetch the post id,title,content,slug,categories and featured_image through this code by creating simple plugin. I am obtained category id in the Code.I want the category name i tried to do this by get_cat_name But did not understand it. How can i get category name,author name and author profile image through custom endpoint. I am also a beginner in wordpress. I refer to the documentation but did not understand how to do it. Write it as in w_posts function for category name
$data[$i]['catname']= get_cat_name($data[$i]['categories']);
<?php
/** Plugin Name : Custom API
* Plugin URI : https://google.com
* Decription : Crushing It
* Version : 1.0
* Author: Shahryar
* Author URI: https://google.com
*/
/** Plugin Name: Custom API... */
function w_posts(){
$args = [
'numberposts'=> 99999,
'post_type' => 'post'
];
$posts = get_posts($args);
$data = [];
$i = 0;
foreach($posts as $post) {
$data[$i]['id'] = $post->ID;
$data[$i]['title'] = $post->post_title;
$data[$i]['content'] = $post->post_content;
$data[$i]['slug'] = $post->post_name;
$data[$i]['categories'] = $post->post_category;
$data[$i]['featured_image']['thumbnail'] = get_the_post_thumbnail_url($post->ID, 'thumbnail');
$data[$i]['featured_image']['medium'] = get_the_post_thumbnail_url($post->ID, 'medium');
$data[$i]['featured_image']['large'] = get_the_post_thumbnail_url($post->ID, 'large');
$i++;
}
return $data;
}
function my_awesome_func( $data ) {
$posts = get_posts( array(
'author' => $data['id'],
) );
if ( empty( $posts ) ) {
return null;
}
return $posts[0]->post_title;
}
function my_awesome_function( $data ) {
$posts = get_posts( array(
'author' => $data['id'],
) );
if ( empty( $posts ) ) {
return null;
}
return $posts[0]->post_title;
}
function w_post( $slug ) {
$args = [
'name' => $slug['slug'],
'post_type' => 'post'
];
$post = get_posts($args);
$data['id'] = $post[0]->ID;
$data['title'] = $post[0]->post_title;
$data['content'] = $post[0]->post_content;
$data['slug'] = $post[0]->post_name;
$data['categories'] = $post[0]->post_category;
$data['featured_image']['thumbnail'] = get_the_post_thumbnail_url($post[0]->ID, 'thumbnail');
$data['featured_image']['medium'] = get_the_post_thumbnail_url($post[0]->ID, 'medium');
$data['featured_image']['large'] = get_the_post_thumbnail_url($post[0]->ID, 'large');
return $data;
}
add_action('rest_api_init',function(){
register_rest_route('w/v1','posts',[
'methods'=> 'GET',
'callback'=>'w_posts',
]);
register_rest_route( 'w/v1', 'posts/(?P<slug>[a-zA-Z0-9-]+)', array(
'methods' => 'GET',
'callback' => 'w_post',
) );
});
You are getting category id then you can get category name using it' id like:
get_the_category_by_ID( $cat_ID )
To get the author name and image you can do as following:
$author_id=$post->post_author; //get author id
//get author image URL by author id
$author_img_url = the_author_meta( 'avatar' , $author_id );
//get author name by author id
the_author_meta( 'user_nicename' , $author_id );

Wordpress find category of custom post connected to custom post

Below is my working code to change a custom text field in every post of type lp_lesson to some text. Each one of these lp_lessons are assigned a specific post (lp_course).
My question is how do I figure out which lesson is connected to which course? I have a space in my phpAdmin title wp_learnpress_section_items that have a section_id and an item_id but I don't know how to access these as I have tried: $field_value = get_post_meta( $post->ID, 'section_id', 1);
The section_id references an array in my phpAdmin of wp_learnpress_sections and I would love to do this:
get the item_id which is equivalent to the post's ID. Then get the section_id associated with that item_id. Then get the section_course_id associated with that section_id. I have the logic down - I just don't know how to access these variables or items. I am guessing my $field_value is not the correct way to access these items.
add_action( 'wp_enqueue_scripts', 'win_9388244_format_lp_lesson' );
function win_9388244_format_lp_lesson() {
//Get post type of lp_lesson
$args = array(
'post_type' => 'lp_lesson',
'numberposts' => 99999
);
$posts = get_posts($args);
foreach($posts as $post) {
//if category == Test
update_post_meta( $post->ID, 'wpk_icon_text', 'Test' );
}
}
Edit: I have found that $post->ID gives me the post's ID, but not the rest of the variables.
For anyone who wants to do something similar I got it to work.
function win_9388244_format_lp_lesson() {
//Get post type of lp_lesson
$args = array(
'post_type' => 'lp_lesson',
'numberposts' => 99999
);
$posts = get_posts($args);
global $wpdb;
$sections = $wpdb->get_results( "SELECT section_item_id, section_id, item_id FROM wp_learnpress_section_items", ARRAY_A );
$items = $wpdb->get_results( "SELECT section_course_id, section_id FROM wp_learnpress_sections", ARRAY_A );
foreach($posts as $post) {
$lesson_id = $post->ID;
foreach($sections as $section) {
if($section['item_id'] == $lesson_id) {
$currentSection = $section['section_id'];
foreach($items as $item) {
if ($item['section_id'] == $currentSection) {
$course_code = $item['section_course_id'];
if ($course_code == 6177){
update_post_meta( $post->ID, 'wpk_icon_text', 'Time & Labor' );
}
else{
update_post_meta( $post->ID, 'wpk_icon_text', 'Lesson' );
}
}
}
}
}
}
}

Get array of items from phpAdmin Wordpress

In my phpAdmin I have a list of arrays that I need to be able to access in php Wordpress.
I need to get these two arrays and the variables associated with them but I can't find anywhere how to access this type of information.
Essentially I would like to loop through all these items and match one of their variables with the ID of specific posts - I have the post part.
wp_learnpress_sections
wp_learnpress_section_items
Basically the item_type is an lp_lesson which is a custom post type. I am able to grab all the posts from wp_posts so I figured I would be able to grab these other arrays?
Edit:
My theme function. This works for all the posts. However, I want to be able to find out which section_id a post belongs to.
add_action( 'wp_enqueue_scripts', 'win_9388244_format_lp_lesson' );
function win_9388244_format_lp_lesson() {
//Get post type of lp_lesson
$args = array(
'post_type' => 'lp_lesson',
'numberposts' => 99999
);
$posts = get_posts($args);
foreach($posts as $post) {
update_post_meta( $post->ID, 'wpk_icon_text', 'Test' );
}
}
Full function that does it:
function win_9388244_format_lp_lesson() {
//Get post type of lp_lesson
$args = array(
'post_type' => 'lp_lesson',
'numberposts' => 99999
);
$posts = get_posts($args);
global $wpdb;
$sections = $wpdb->get_results( "SELECT section_item_id, section_id, item_id FROM wp_learnpress_section_items", ARRAY_A );
$items = $wpdb->get_results( "SELECT section_course_id, section_id FROM wp_learnpress_sections", ARRAY_A );
foreach($posts as $post) {
$lesson_id = $post->ID;
foreach($sections as $section) {
if($section['item_id'] == $lesson_id) {
$currentSection = $section['section_id'];
foreach($items as $item) {
if ($item['section_id'] == $currentSection) {
$course = $item['section_course_id'];
//switch $course with predefined variables for courses
}
}
}
}
}
}

WordPress: Get checked/selected taxonomy label

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.

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

Categories