So I'm using wordpress and ACF. I created a CPT called products and a custom taxonomy named product_types. I have a loop that displays all product_type sections and each section contains products tied to it. So I'm looking for ways to easily filter new products or popular products thats why I come up with adding a dedicated category for it but I dont want it to exclude it from my post loop. pls see img attached
enter image description here
Here's the code I'm working with atm
<?php
$cpt_name = 'products';
$taxonomy_name = 'product_type';
// Retrieve the terms in the above taxonomy.
$terms = get_terms( $taxonomy_name );
if ( empty( $terms ) || is_wp_error( $terms ) ) {
return;
}
echo '<div class="products-group">';
foreach( $terms as $term ) {
echo '<div class="products-category">';
echo '<h2 class="title-category">' . $term->name . '</h2>';
$args = array(
'post_type' => $cpt_name,
'tax_query' => array(
array(
'taxonomy' => $taxonomy_name,
'field' => 'slug',
'terms' => array( $term->slug ),
),
),
'posts_per_page' => -1,
'category__not_in' => array( 30 ),
);
$query = new WP_Query( $args );
echo '<div class="products-list">';
while ( $query->have_posts() ) {
$query->the_post();
$attachment_id = get_field('header_bg_image');
$size = "wide-thumbnail"; // (thumbnail, medium, large, full or custom size)
$image = wp_get_attachment_image_src( $attachment_id, $size );
if ( ! empty( $image ) ) {
$image_html = esc_url( $image[0] );
} else {
$image_html = 'https://rasons.ltd/wp-content/uploads/2020/02/interior-exterior-finish-800x400.jpg';
}
printf( '%s', get_the_permalink(), esc_attr( the_title_attribute( 'echo=0' ) ), get_the_title() );
}
echo '</ul>';
wp_reset_query();
echo '</div></div>';
} // end foreach.
echo '</div>';
?>
Related
Hi I am trying to display the child term of the current post category page.
However I can figure out what am missing here. Thanks
<?php
$current_tax = get_query_var( 'category' );
$current_term = get_queried_object()->term_id;
$child_terms = get_terms( array('category' => $current_tax,
'child_of' => $current_term,
'orderby' => 'name' ));
if ( ! empty( $child_terms ) && ! is_wp_error( $child_terms ) ) {
echo '<div class="post-index__cate">';
foreach( $child_terms as $child ) {
printf( '<a class="category-name" href="%s">', get_term_link( $child, $child->taxonomy ));
echo $child->name. '</a>';
}
echo '</div>';
}
?>
In your code the taxonomy should be “category”, so your arguments for get_terms() should have “taxonomy” => “category”. $current_tax is irrelevant in this code then.
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;
}
}
I'm trying to display the taxonomy images into front-end, But I don't get how to use the code. Here I'm using a plugin Taxonomy Images [https://wordpress.org/plugins/taxonomy-images/] Please, can someone help me?
Here is my code below:
<?php $terms = get_terms('category');
foreach($terms as $term):
$args = array(
'orderby' => 'title',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => array($term->slug)
)
));
$tag_query = new WP_Query( $args );
if($tag_query->have_posts()):
$image_url = apply_filters( 'taxonomy-images-queried-term-image-url', '' );
echo '<li>
' . wp_get_attachment_image( $term->image_id, 'detail' ) . '
'.esc_html($term->name).'
<p>'.esc_html($term->description).'</p>
</li>';
while($tag_query->have_posts()):$tag_query->the_post();
endwhile;
endif;
wp_reset_postdata();
endforeach;
?>
If you are using Taxonomy image plugin then use wp_get_attachment_image method to get the image.
Below is complete code for you.
check below code this will help you.
$terms = apply_filters( 'taxonomy-images-get-terms', '' );
if ( ! empty( $terms ) ) {
print '<ul>';
foreach ( (array) $terms as $term ) {
print '<li>' . wp_get_attachment_image( $term->image_id, 'detail' ) . ' <a href="#">'.esc_html($term->name).'
<p>'.esc_html($term->description).'</p></li>';
}
print '</ul>';
}
Use wp_get_attachment_image() funxtion to show taxnomy images at front-end.
wp_get_attachment_image( $term->image_id, 'detail' )
Note: Pass image_id in wp_get_attachment_image() function.
Reference
Updated Answer:
Yes, You can also get category name and description inside the loop.
$term->name
$term->description
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();
}
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.