Get custom taxonomy's custom meta from outside the loop - php

I've created a number of custom fields for custom taxonomy. Got no problem pulling their values inside the query. But in this case, I get them repeating as many times as I have posts in this taxonomy.
This is the code with it inside the loop
$tables_terms = $atts['custom'];
$tabargs = array(
'posts_per_page' => -1,
'offset' => 0,
'post_type' => 'customtables',
'tax_query' => array(
array(
'taxonomy' => 'tables',
'field' => 'slug',
'terms' => array(
$tables_terms
)
)
)
);
$tabs = new WP_Query( $tabargs );
if( $tabs->have_posts() ){
while( $tabs->have_posts() ) : $tabs->the_post();
$terms = get_the_terms( get_the_ID(), 'tables' );
foreach ( $terms as $term ) {
$t_id = $term->term_id;
$term_meta = get_option( "taxonomy_$t_id" );
echo $term_meta['term_1'];
}
endwhile;
wp_reset_postdata();
echo $custom;
}
How do I get these to show once, outside the loop?

You can use, get_term_meta;
get_term_meta retrieves metadata for a term.
get_term_meta( $term->term_id, 'your_term_name', true )

So the following code actually worked for me
$term_slug = $tables_terms;
$taxonomies = get_taxonomies();
foreach ( $taxonomies as $tax_type_key => $taxonomy ) {
if ( $term_object = get_term_by( 'slug', $term_slug , $taxonomy ) ) {
break;
}
}
$t_id = $term_object->term_id;
$term_meta = get_option( "taxonomy_$t_id" );
and from there on, I simply echoed each meta like this
echo $term_meta['term_1'];

Try Like that:
Store your output in a array and check when store it that value already exist or not.
$tables_terms = $atts['custom'];
$tabargs = array(
'posts_per_page' => -1,
'offset' => 0,
'post_type' => 'customtables',
'tax_query' => array(
array(
'taxonomy' => 'tables',
'field' => 'slug',
'terms' => array(
$tables_terms
)
)
)
);
$tabs = new WP_Query( $tabargs );
$term_meta_array = array();
if( $tabs->have_posts() ){
while( $tabs->have_posts() ) : $tabs->the_post();
$terms = get_the_terms( get_the_ID(), 'tables' );
foreach ( $terms as $term ) {
$t_id = $term->term_id;
$term_meta = get_option( "taxonomy_$t_id" );
echo $term_meta['term_1'];
if(!in_array($term_meta['term_1'], $term_meta_array)) array_push($$term_meta_array, $term_meta['term_1']);
}
endwhile;
wp_reset_postdata();
echo $custom;
foreach($term_meta_array as $st){
echo $st;
}
}

Related

wp_set_object_terms not setting the terms for the post

This code does not set the object terms. Please help me sort out the issue. The code results in the posts list and doesn't set the terms.
I have checked the below data.
The query results in the list of post.
calculation part in the loop results in the value (lesser or greater than 0)
function set_expired_job_categories() {
global $post;
$current_time = time();
$taxonomy = 'current-status';
$job_expired_id = 368;
$job_ongoing_id = 367;
// Set our query arguments
$args = array(
'fields' => 'ids', // Only get post ID's to improve performance
'post_type' => 'job', // Post type
'post_status' => 'publish',
'posts_per_page' => -1,
'tax_query' => array(
'taxonomy' => 'current-status',
'field' => 'slug',
'terms' => array( 'ongoing' ),
),
);
$job_expiration_query = new WP_Query( $args );
// Check if we have posts to delete, if not, return false
if( $job_expiration_query->have_posts() ){
while( $job_expiration_query->have_posts() ){
$job_expiration_query->the_post();
$postid = get_the_ID();
$expire_timestamp = rwmb_meta( 'deadline_date' );
if ( $expire_timestamp ) {
$seconds_between = ( (int)$expire_timestamp - (int)$current_time );
if ( $seconds_between <= 0 ) {
wp_set_object_terms( $postid, (int)$job_expired_id, $taxonomy, true );
wp_remove_object_terms( $postid, (int)$job_ongoing_id, $taxonomy );
}
}
}wp_reset_postdata();
}
}
add_action( 'set_job_categories', 'set_expired_job_categories', 20, 2 );
As it is in the functions file, I need to set the global $wp_taxonomies to populate the taxonomies data initially. Also, instead of using the tag_ID, I have revised with the slug. These two changes helped to work out the code. The revised code is below for reference.
Thank you all for your efforts.
function set_expired_job_categories() {
global $post;
global $wp_taxonomies;
$current_time = time();
$taxonomy = 'current-status';
$job_expired_id = 'expired';
$job_ongoing_id = 'ongoing';
// Set our query arguments
$args = array(
'fields' => 'ids', // Only get post ID's to improve performance
'post_type' => 'job', // Post type
'post_status' => 'publish',
'posts_per_page' => -1,
'tax_query' => array(
'taxonomy' => 'current-status',
'field' => 'slug',
'terms' => array( 'ongoing' ),
),
);
$job_expiration_query = new WP_Query( $args );
// Check if we have posts to set categories, if not, return false
if( $job_expiration_query->have_posts() ){
while( $job_expiration_query->have_posts() ){
$job_expiration_query->the_post();
$postid = get_the_ID();
$expire_timestamp = rwmb_meta( 'deadline_date' );
if ( $expire_timestamp ) {
$seconds_between = ( (int)$expire_timestamp - (int)$current_time );
if ( $seconds_between >= 0 ) {
}else {
wp_set_object_terms( $postid, (int)$job_expired_id, $taxonomy, true );
wp_remove_object_terms( $postid, (int)$job_ongoing_id, $taxonomy );
}
}
}
wp_reset_postdata();
}
}
// hook it to low priority value, due to CPT and Taxonomies
add_action( 'set_job_categories', 'set_expired_job_categories', 20, 2 );
Reference: https://wordpress.org/support/topic/wp_set_object_terms-in-loop-is-not-work-in-taxonomy-cpt/

Display posts in the order of their selection

I've offert the possibility to my client to choose one by one the related post for each post. I've set up a custom field with ACF and use this to display the choosen posts or random posts :
<?php $terms = get_the_terms( $post->ID , 'custom-taxonomy1' ); if ( !empty( $terms ) ) :?>
<?php
$related_acf = get_field('related_group');
$rel_posts = $related_acf["related posts"];
$related_ids = array();
foreach ( $rel_posts as $rel_post) {
array_push($related_ids, $rel_post['post']);
}
global $post;
$current_post_type = get_post_type( $post );
$terms = get_the_terms( $post->ID , 'custom-taxonomy1' );
$term_ids = array_values( wp_list_pluck( $terms,'term_id' ) );
$post_count_skrn = get_theme_mod('progression_studios_media_more_like_post_count', '3');
if ( count($related_ids) > 0 ) {
$args=array(
'post_type' => array('custompost1', 'custompost2', 'post'),
'post__in' => $related_ids
);
} else {
$args=array(
'post_type' => $current_post_type,
'posts_per_page'=> $post_count_skrn,
'orderby'=>'rand', // Randomize the posts
'post__not_in' => array( $post->ID ),
'tax_query' => array(
array(
'taxonomy' => 'video-genres',
'terms' => $term_ids,
'field' => 'id',
'operator' => 'IN'
)
),
);
}
So I wonder if I can add something like :
if ( count($related_ids) > 0 ) {
$args=array(
'post_type' => array('custompost1', 'custompost2', 'post'),
'orderby'=>'XXXXX', // Ordering the posts with the order of selection
'post__in' => $related_ids
);
Best regards,
Clément
Wordpress have 'orderby'=>'post__in'
Posts will now be displayed according to their position in the $related_ids array
$args=array(
'post_type' => array('custompost1', 'custompost2', 'post'),
'orderby'=>'post__in', // Ordering the posts with the order of selection
'post__in' => $related_ids
);

How to search for a string in a category name?

I use the search result and I GET
$s = $_GET['s'];
Which gives: "mario"
Now there is a category named Mario Bianchi under a main category called Persone
When I GET $s I need to get all posts within that category, I tried the following but I get nothing
$terms = get_terms( 'category', array(
'name__like' => $s,
'hide_empty' => true // Optional
) );
if ( count($terms) > 0 ){
echo '<ul>';
foreach ( $terms as $term ) {
echo '<li>' . esc_html( $term->name ) . '</li>';
}
echo '</ul>';
}
Yet I need the actual post attached, not the category itself
get_terms does just that, it get the terms for your query. What you'll want is WP_Query with a tax_query. You've already got the categories you want to return the posts for, so it shouldn't be too difficult
$terms = get_terms( array(
'taxonomy' => 'category',
'name__like' => $s,
'hide_empty' => true // Optional
) );
$term_ids = array();
if ( ! empty( $terms ) ) {
foreach( $terms as $term ) {
$term_ids[] = $term->term_id;
}
$args = array(
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'category',
'terms' => $term_ids,
),
),
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
echo '<ul>';
while ( $query->have_posts() ) {
$query->the_post();
echo '<li>' . esc_html( get_the_title() ) . '</li>';
}
echo '</ul>';
}
wp_reset_postdata();
}

Excluding duplicates from a foreach loop in a WP_Query for WooCommerce products

I have a WP Query to list matching products, like this:
function sku_ean_sizes () {
global $product;
$current_stijlcode = $product->get_attribute( 'pa_stijlcode' );
$current_ean = $product->get_attribute( 'pa_ean' );
$postid = get_the_ID();
$args = array(
'post_type' => 'product',
'orderby' => 'meta_value_num',
'meta_key' => '_price',
'order' => 'asc',
'posts_per_page' => 100,
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'pa_stijlcode',
'field' => 'slug',
'terms' => $current_stijlcode,
),
array(
'taxonomy' => 'pa_ean',
'field' => 'slug',
'terms' => $current_ean,
)
)
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
global $product;
foreach( wc_get_product_terms( $product->id, 'pa_maat' ) as $attribute_value ){
echo '<span>' . $attribute_value . '</span>';
}
endwhile;
} else {
// no sku matches found
}
wp_reset_postdata();
}
I would like to use the foreach loop to list the found product attributes attached to the products found with the WP Query:
global $product;
foreach( wc_get_product_terms( $product->id, 'pa_maat' ) as $attribute_value ){
echo '<span>' . $attribute_value . '</span>';
}
This code works. However, the $attribute_value variable outputs duplicate records. This makes sense, as multiple products could have the same output.
What adjustment can i make so it excludes duplicate values?
In context; This is to display all available sizes for a specific product.
Updated
You should use a foreach loop to set all the values in an array avoiding duplicates
Then, using implode() PHP function, you will display all attributes term names without duplicates.
This partial code to replace yours, will display the non duplicated attribute term names for all products at once:
$loop = new WP_Query( $args );
if ( $loop->have_posts() ):
$maat_term_names = array();
while ( $loop->have_posts() ):
$loop->the_post();
// Set the attribute term names in an array avoiding duplicates
foreach( wc_get_product_terms( $loop->post->ID, 'pa_maat' ) as $attribute_value ):
$maat_term_names[$attribute_value] = $attribute_value;
endforeach;
endwhile;
// Sorting (ordering)
sort($maat_term_names);
// Here you display attribute term names without duplicates (coma separated)
echo '<span>' . implode( '</span>, <span>', $maat_term_names ) . '</span> ';
else:
echo '<span>No SKUs matches found</span>';
endif;
wp_reset_postdata();

WordPress/Woocommerce: How to get specific child category by 'id' from parent category ID

I found a nice code(shown below) from this Website.
It works great but I need to get a specific child category by 'id'.
For example, if the output from the code is:
Red
Blue
Green
Yellow
How to get only Green because I need to make another query to use it in
'tax_query'=>array('field'=>'id')
Here is the function:
//woocommerce get sub categories from parent id
function woocommerce_subcats_from_parentcat_by_ID($parent_cat_ID) {
$args = array(
'hierarchical' => 1,
'show_option_none' => '',
'hide_empty' => 0,
'parent' => $parent_cat_ID,
'taxonomy' => 'product_cat'
);
$subcats = get_categories($args);
foreach ($subcats as $sc) {
$link = get_term_link( $sc->slug, $sc->taxonomy );
echo $sc->name.'</br>';
}
}
Here is the code where I have to get the specific id of the category:
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
global $product;
$product = $cart_item['data'];
if ( has_term( 'phone-model', 'product_cat', $product->id ) ) {
$cat_check = true;
$term_list = wp_get_post_terms( $product->id,'product_cat',array('fields'=>'ids'));
$cat_id = (int)$term_list[0];
$funspecificsub = woocommerce_subcats_from_parentcat_by_ID($cat_id);
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id', //This is optional, as it defaults to 'term_id'
'terms' => $funspecificsub,
'include_children' =>true
)
)
);
$loop = new WP_Query( $args );
$i=1;
while ( $loop->have_posts() ) : $loop->the_post();
global $product;
if($product->is_visible()){
echo '<li style=">';
echo '<a id="cover_'.$i.'" class=" '.$product->id.'" >';
echo '<div class="">'.get_site_url().'/?add-to-cart='.$product->id.'</div>';
echo '<h5>'.get_the_title().'</h5>';
echo '<h6>'.wc_price($product->get_price_including_tax(1,$product->get_price())).'</h6>';
echo '</a>';
echo '</li>';
}else{}
$i++;
endwhile;
wp_reset_query();
}
}
So I guess the main question is how do I get 'Green' category id in variable $funspecificsub above. At the moment its outputting all the Sub categories. I want to be able to select specific Sub Category.

Categories