my taxonomy-$taxonomy.php page post not show? - php

I created a custom post taxonomy.Now i want to show all specific post by specific taxonomy. so I created a taxonomy-product_cat.php page.
here is product page get term and link code--
<div class="side_box side_box_1 red5">
<h5>Filter Products</h5>
<h6>Brand</h6>
<?php $topics = get_terms('product_cat');
echo '<ul class="advanced-filters tgl_c">';
foreach ($topics as $topic) {
echo '<li class="advanced-filter" data-group="Brand">'.$topic->name.'</li>';
}
echo '</ul>';?>
</div>
here is custom post taxonomy code---
function product_taxonomy() {
register_taxonomy(
'product_cat', //The name of the taxonomy. Name should be in slug form (must not contain capital letters or spaces).
'product', //post type name
array(
'hierarchical' => true,
'label' => 'product Category', //Display name
'query_var' => true,
'show_admin_column' => true,
'rewrite' => array(
'slug' => 'product-category', // This controls the base slug that will display before each term
'with_front' => false // Don't display the category base before
)
)
);
}
add_action( 'init', 'product_taxonomy');
And here is taxonomy-product_cat.php page code--
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); $unique = "_product_"; ?>
<div class="col-md-3 col-xs-6 element mb30">
<div class="main_box">
<div class="box_1">
<div class="product-image">
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail( $post->ID, 'product-image',array('class' => 'img-responsive') );?>
</a>
</div>
<div class="overlay hidden-sm hidden-xs">
More Info
</div>
</div>
<div class="desc">
<h5><?php the_title(); ?></h5>
<p><?php echo get_post_meta(get_the_ID(),$unique.'brand_name',true); ?></p>
</div>
</div>
</div>
<?php endwhile; ?>
<?php else :?>
<h3><?php _e( 'Not Found Any Product.' ); ?></h3>
<?php endif ?>
But the result is Not Found Any Product.So please someone help me how can i fix this problem.Thanks

see documentation
https://developer.wordpress.org/reference/functions/get_terms/
specifically
Since 4.5.0, taxonomies should be passed via the ‘taxonomy’ argument in the $args array:
$terms = get_terms( array(
'taxonomy' => 'post_tag',
'hide_empty' => false,
) );
instead of the first parameter being the taxonomies.
get_terms('product_cat');
You should do
get_terms(array(
'taxonomy' => 'product_cat'
));
Not sure if that is the issue, but seems the most obvious thing.
UPDATE
Did you try this
get_terms(array(
'taxonomy' => 'product_cat',
'hide_empty' => 0
));
This will show the taxonomy if you didn't insert any actual terms with
wp_insert_term()
As per this page,
https://wordpress.org/support/topic/get_terms-does-not-return-my-custom-taxonomy-terms
I don't typically work with WP on this level, but my google skills are unmatched ... lol

Related

Woocommerce product filtering wp query

A client wants only variable products to be shown as single products on category pages, and we need filter for that, i created wp-query to get those products on category pages
Custom query example for category page:
<?php //showing products by category
$cat_name = 'sommerbettdecke'; // Product category name
$args = array(
'post_type' => 'product_variation',
'post_status' => 'publish',
'order' => 'ASC',
'posts_per_page' => -1,
'post_parent__in' => get_variation_parent_ids_from_term( $cat_name, 'product_cat', 'name' ), // Variations
'tax_query' => array( array(
'taxonomy' => 'pa_jahreszeit',
'field' => 'slug',
'terms' => 'sommer',
'operator' => 'IN',
) )
);
$wc_query = new WP_Query($args);?>
<p class="product-counter">Products on page <strong><?php echo $wc_query->post_count?></strong></p>
<ul class="products">
<!-- count all products on page -->
<?php if ($wc_query->have_posts()) :
while ($wc_query->have_posts()) :
$wc_query->the_post(); ?>
<!-- get attributes -->
<?php global $product; ?>
<?php $pa_fullung = array_shift( wc_get_product_terms( $product->id, 'pa_fullung', array( 'fields' => 'names' ) ) ); ?>
<?php $pa_grose = array_shift( wc_get_product_terms( $product->id, 'pa_grose', array( 'fields' => 'names' ) ) ); ?>
<!-- get product image -->
<?php $attachment_ids[0] = get_post_thumbnail_id( $product->id ); ?>
<?php $attachment = wp_get_attachment_image_src($attachment_ids[0], 'full' ); ?>
<!-- product display start here -->
<li class="product">
<a href="<?php the_permalink(); ?>">
<?php if ( has_post_thumbnail( $product->id ) ) { ?>
<img src="<?php echo $attachment[0] ; ?>" class="card-image" />
<?php } ?>
<div class="produt-cat-loop">
<h2><?php the_title(); ?></h2>
<table>
<tbody><tr>
<td>Füllung:</td>
<td><?php echo $pa_fullung ?></td>
</tr>
<tr>
<td>Füllgewicht:</td>
<td><?php echo $pa_grose ?></td>
</tr>
<tr>
<td>Hülle:</td>
<td>Definisati atribut</td>
</tr>
</tbody>
</table>
</div>
<button class="btn-orange">Zum Produkt</button>
</a>
</li>
<!-- product display ends here -->
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<!-- no products message -->
<p><?php _e( 'No Products' ); ?></p>
<?php endif; ?>
</ul>
I had to list them by category and by the attribute to get it right way. Now I have to get a sidebar filter for those products, default woocommerce product filter doesn't work with custom queries. Is there any plugin that could do the work with custom query, or is there any way to get variable products as single products and to get products on category pages by category and attribute in default woocommerce loop?
when listing products on category page i have to list them not only by the category slug but also by the attribute(pa_jahreszeit) value, in this query example that attribute is 'pa_jahreszeit' and its value is 'sommer' - not sure if this can be done by default woocommerce loop from archive-product.php
Update:
-in sidebar I need to filter products by attribtes and price
Thank you in advance, sorry for misspelling
Update vol.2:
Here is a code from functions.php - using pre_get_posts
add_action( 'pre_get_posts', 'rc_modify_query_get_design_projects' );
function rc_modify_query_get_design_projects( $product ) {
$product->set('post_type', 'product_variation');
if (is_category( '2312' ) ) {
$taxquery = array(
array(
'taxonomy' => 'pa_jahreszeit',
'field' => 'slug',
'terms' => 'sommer',
'operator' => 'IN'
)
);
$product->set( 'tax_query', $taxquery );
}
}
When i put taxquery outside of if (is_category( '2312' ) ) { it works fine, but while the taxquery is inside this condition it doesnt work, it shows all variable products :/

Exclude specific Woocommerce product category term(s) from a WP Query

I'm trying to load latest product in Woocommerce (on home page). I want to exclude specific product category from display on the loop, but exclude product category id is not working for me.
What I have done so far:
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => 12,
'post_status' => 'publish',
'taxonomy' => 'product_cat',
'exclude' => 29,//exclude mention category id from loop
'parent' => 0
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
global $product;
?>
<div class="popular-inner">
<a href="<?php echo get_permalink(); ?>">
<div class="popular-image d-flex ">
<div class="align-self-center mx-auto ">
<?php the_post_thumbnail(); ?>
</div>
</div>
<div class="popular-content-wp">
<div class="popular-title">
<h6><?php echo get_the_title(); ?></h6>
</div>
<div class="popular-price">
<p><?php echo wc_price($product->get_price()); ?></p>
</div>
<div class="popular-add-to-cart">
<ul>
<li>
Add to Cart
</li>
</ul>
</div>
</div>
</a>
</div>
<?php endwhile; wp_reset_query();?>
Since wordpress version 3.1 using a taxonomy slug in a WP_Query is deprecated in flavor of tax_query. Also there are some other mistakes. See WP_Query taxonomy parameters.
Your revisited code:
<?php
$loop = new WP_Query( array(
'post_type' => 'product',
'posts_per_page' => 12,
'post_status' => 'publish',
'parent' => 0,
'tax_query' => array( array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => array( 29 ), // Term ids to be excluded
'operator' => 'NOT IN' // Excluding terms
) ),
) );
if ( $loop->have_posts() ) :
while ( $loop->have_posts() ) : $loop->the_post();
// Get the instance of the WC_Product from the post ID
$product = wc_get_product( get_the_id() );
?>
<div class="popular-inner">
<a href="<?php echo get_permalink(); ?>">
<div class="popular-image d-flex ">
<div class="align-self-center mx-auto ">
<?php the_post_thumbnail(); ?>
</div>
</div>
<div class="popular-content-wp">
<div class="popular-title">
<h6><?php echo get_the_title(); ?></h6>
</div>
<div class="popular-price">
<p><?php echo wc_price( $product->get_price() ); ?></p>
</div>
<div class="popular-add-to-cart">
<ul>
<li>
Add to Cart
</li>
</ul>
</div>
</div>
</a>
</div>
<?php
endwhile;
wp_reset_postdata();
else: ?>
<p><?php _e( 'No posts found.' ); ?></p>
<?php endif;
?>
It should work now.

Wordpress - How do I display posts grouped by custom taxonomy?

I am creating a FAQ page using a custom post type and custom taxonomy. I'm trying to create an unordered list for each taxonomy in order to group the FAQ's. In that unordered list, I want the first listed item to be the taxonomy name and then repeat the second listed item for all the questions in the taxonomy. This is the page I'm working on link.
It's currently duplicating the posts instead of displaying in the rightful taxonomies.
<?php
// get all the categories from the database
$cats = get_terms( array(
'taxonomy' => 'faq_categories',
));
// loop through the categories
foreach ($cats as $cat) {
// setup the category ID
$cat_id = $cat->term_id;
?>
<!-- Make a header for the category -->
<ul id="<?php echo $cat->slug; ?>" class="cd-faq-group">
<li class="cd-faq-title">
<h2>Questions <?php echo $cat->name; ?></h2>
</li>
<?php
// create a custom wordpress query
query_posts( array(
'post_type' => 'faqs',
'tax_query' => array(
array(
'taxonomy' => 'faq_categories', //or tag or custom taxonomy
'field' => 'slug',
'terms' => 'for-women'
)
)
));
// start the wordpress loop!
if (have_posts()) : while (have_posts()) : the_post(); ?>
<li>
<a class="cd-faq-trigger" href="#0"><?php the_title(); ?></a>
<div class="cd-faq-content">
<?php the_content(); ?>
</div>
</li>
<?php endwhile; endif; // done our wordpress loop. Will start again for each category
wp_reset_postdata();
?>
</ul>
<?php } // done the foreach statement ?>
Your query is not changing as you iterate through the $cats array. Perhaps changing the the value of the 'terms' array to $cat->slug would give you better results.
Thank you so much. You have both provided great insight as to what I was missing. I've resolved it now and this is how I went about solving it taking your suggestions into consideration.
<?php
$cats = get_terms(
array(
'taxonomy' => 'faq_categories',
'orderby' => 'term_id',
'order' => 'ASC'
)
);
foreach ($cats as $cat) :
?>
<ul id="<?php echo $cat->slug; ?>" class="cd-faq-group">
<li class="cd-faq-title">
<h2>Questions <?php echo $cat->name; ?></h2>
</li>
<?php
$questions = new WP_Query(
array(
'category_name' => $cat->slug
)
);
$questions = new WP_Query( array(
'post_type' => 'faqs',
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'faq_categories',
'field' => 'slug',
'terms' => array($cat->slug),
)
)
));
?>
<?php if ($questions->have_posts()) : while ($questions->have_posts()) : $questions->the_post();?>
<li>
<a class="cd-faq-trigger" href="#0"><?php the_title(); ?></a>
<div class="cd-faq-content">
<?php the_content(); ?>
</div>
</li>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
In your query_post, your tax_query's field should be term_id and your terms be assigned to your $cat_id variable, instead of a hardcoded term.

One post in two archives

Well, this one is getting me quite frustrated. I have multiple custom post types (CPT). Most of them have an archive. Now I have a ctp called Portfolio and a cpt called recent work.
What I would like is to have both ctp's show up in the archives-[slug].php pages. BUT in one archive the link should be /portfolio/page-name and in the other it should be /recent-work/page-name.
Both pages would show the same content only the archive slug would be different. I don't want to have to create double content.
Does anyone know if this is possible using Wordpress and how I can achieve this...
As Gavin Thomas said, use categories (well in my case custom taxonomies). This is my loop code:
<?php $args = array(
'post_type' => 'portfolio',
'posts_per_page'=> -1,
'orderby' => 'ID',
'order' => 'DESC',
'tax_query' => array(
array(
'taxonomy' => 'event-type',
'field' => 'slug',
'terms' => 'corporate-events'
)
)
);
$products = new WP_Query( $args );
if( $products->have_posts() ) {
while( $products->have_posts() ) {
$products->the_post();
?>
<?php $naam = get_field('naambedrijf');
$soort = get_field('soort_uitje');
$foto = get_field('flickr_fotoset'); ?>
<div class="col s6 m4 l4">
<a href="<?php the_permalink(); ?>">
<div class="title">
<h2 class="truncate" style="line-height:20px;"><?php echo $naam; ?></h2>
<small class="truncate" style="color:#222;"><?php echo $soort; ?></small>
</div>
<div class="thumb">
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail();
} ;?>
</div>
</a>
</div>
<?php
}
}
else {
echo 'There seems to be a problem, please try searching again or contact customer support!';
} ?>
The items under the ctp are displayed using this code, but the permalink is still /portfolio/page-name and not /category-slug/page-name. How do I go about this?

display content using get posts from terms under a custom taxonomy

I am having a custom taxonomy say "project-type" which is registered for the custom post "projects" and under that I have terms "catOne" and "catTwo". Now I want to display all the custom posts that are linked to catOne using the term_id of "catOne", which in my case is 9.
So I am successfully able to loop through all the posts but it is displaying only the ID but not all contents.
My approach:
$cat_id = 9;
$args = array(
'post_type' => 'projects',
'tax_query' => array(
array(
'taxonomy' => 'project-type',
'field' => 'term_id',
'terms' => array( $cat_id )
),
),
);
$posts = get_posts( $args );
foreach ( $posts as $post ) {
setup_postdata( $post ); ?>
<div id="post-<?php echo $post->ID; ?> <?php post_class(); ?>">
<h1 class="posttitle"><?php the_title(); ?></h1>
<div id="post-content">
<?php the_excerpt(); ?>
</div>
</div>
<?php } wp_reset_postdata();
Output I am getting
<div id="post-137 class=" ""="">
<h1 class="posttitle"></h1>
<div id="post-content">
</div>
</div>
<div id="post-135 class=" ""="">
<h1 class="posttitle"></h1>
<div id="post-content">
</div>
</div>
Can someone please help me with where I am going wrong?
Instead of using post_class(), the_permalink(), the_title(), and the_excerpt(), you should use the $post object to get the data, just like you did with $post->ID. The functions you've used should be called only if you're using a loop based on have_posts(). You aren't, so replace them with:
post_class() -> get_post_class( '', $post->ID )
the_permalink() -> get_the_permalink( $post->ID )
the_title() -> $post->title
the_excerpt() -> $post->post_excerpt

Categories