Get all posts related to a specific taxonomy in WordPress - php

I have created Custom Post Type "Music" and artists taxonomy related to it, I used the below code to list all artists but I what I want is, If any artist was clicked a list of his songs should be shown only.
<?php $terms = get_terms([
'taxonomy' => 'artists',
]);
if (!empty($terms)) {
foreach ($terms as $term) {
echo $term->name;
echo "<br><br>";
}
}
?>
Any one knows how can I display the songs for a specific artist on click?
In other words, how to get the id of the clicked element in order to iterate over the taxonomy and get the specified element?
As far as I am concerned, the elements found inside a taxonomy don't have an id?
Example: The user clicked on Roddy Rich, How to iterate over the taxonomy and get that element?

Related

Display Advanced Custom Field for Woocommerce applied to subcategory on parent category

I have an advanced custom field set up to show on a woocommerce subcategory that allows the user to define a colour via the color picker field.
This field will apply that colour to a number of elements related to that sub category (Styling the sub category thumbnail, the product page itself etc).
Im currently using as per the the ACF documentation this code to pull the field in and display it on the subcategory page:
$term = get_queried_object();
$color = get_field('colour', $term); // Get ACF Field
This works fine until it comes to the parent category for the sub pages. I am unable to call the field in for the sub categories of the parent. I understand I need to use get_terms(). I am unable ot get that working though.
This is some code I found which I have added to the loop on content-product_cat.php. However it just breaks the woocommerce loop. What would I need to do to this code to get the parent category page to show all the child subcategories each with its related color field?
// current term
$current_term = get_queried_object();
// child terms
// this returns an array of terms
$args = array(
'taxonomy' => 'YOUR TAXONOMY HERE',
'parent' => $current_term->term_id,
// you may need other arguments depending on your needs
);
$child_terms = get_terms($args);
// you need to maybe loop through the child terms gotte
// to pick which one you want to use
// I'm assuming that you only want to use the first one
$child_term = false; // set it to false to begin with
// we'll use this later
if ($child_terms) {
$child_term = $child_terms[0];
}
// make a decision
if ($child_term) {
// get field value(s) from child term
$color = get_field('color', $child_term);
} else {
// get field value(s) from current term
$color = get_field('color', $current_term);
}
// do something with the values
echo $color;
I found the solution here:
https://wordpress.stackexchange.com/a/341632
add_action('woocommerce_after_subcategory_title', 'wpse_add_custom_text_under_category_title', 10);
function wpse_add_custom_text_under_category_title($category) {
$term_id = 'product_cat_'.$category->term_id;
the_field('krotki_opis_kategorii', $term_id);
}
From Alex Uleberg:
get_queried_object_id on shop archive page it will return the id of the page and not the category. When you use the hook, the $category object will be passed in through the hook.

How to show more than 1 custom taxonomy term or link on a custom post type?

I have created a custom taxonomy called 'tema' and the taxonomy has three terms. I want to show ALL the term links that are related to the current post. Currently I can only get my code to show ONE of the posts taxonomy terms...
I would like the term links displayed via my custom content.php file ("content-home.php") that I use for showing excerpts of my custom posts on my homepage.
Currently I have this code placed in my custom content.php file and it actually works fine but I can only get it to show ONE term:
<?php
$terms = get_the_terms( $post->ID, 'tema');
foreach($terms as $term) {
echo '<span>' . $term->name . '</span>';
}
?>
Can anyone please show me how I get it to show ALL the posts taxonomy term links?
In the WordPress Codex you can find:
For get_the_terms:
"Retrieve the terms of the taxonomy that are attached to the post."
http://codex.wordpress.org/Function_Reference/get_the_terms
For get_terms:
"Retrieve the terms in a taxonomy or list of taxonomies."
http://codex.wordpress.org/Function_Reference/get_terms
So, get_the_terms() will get the terms (e.g. categories) attached to a post, whereas get_terms() will retrieve the terms in a taxonomy (e.g. categories in the category taxonomy). For example, get_terms( 'category' ) will return all categories that you have added to your WordPress site.
You should use something like this:
<?php
$terms= get_terms(array('taxonomy'=>'tema'));
foreach($terms as $term){
echo '<span>' . $term->name . '</span>';
}
?>
Try below hook function to get list of taxonomy of particular post id,
//Returns All Term Items for "my_taxonomy"
$term_list = wp_get_post_terms($post->ID, 'my_taxonomy', array("fields" => "all"));
print_r($term_list);
*my_taxonomy - replace your taxonomy
https://codex.wordpress.org/Function_Reference/wp_get_post_terms

WooCommerce get parent category from current category page

How can I get the parent category from a sub category page in WooCommerce? After doing extensive research this piece of code looks promising
get_ancestors( $product_cat_id, 'product_cat' );
but it keeps returning 'array' in a loop.
$parentcats = get_ancestors($product_cat_id, 'product_cat');
This is the correct way and you will get one or more parent categories, as an array and loop over it to get the value like below:
foreach($parentcats as $parentcat){
echo $parentcat;
}

How to display number of posts from a tag by id?

Trying
<?php
$number = 'apartamenty';
$terms = get_terms('post_tag', "number=$number");
if($terms){
foreach ($terms as $t){
echo $t->name.' : '.$t->count;
}
}?>
But this code show two tags...
To get the number of posts that are attached to a specific post tag, such as apartamenty, you would use get_term_by(). This WordPress Core construct lets you grab the WP_Term object for the specific post tag. In your case, you are wanting the post tag of apartamenty.
Okay, you'd have a function (i.e. to make it reusable) that runs this code and you'd pass in the actual post tag that you want to explore. This function is using the post tag slug.
function render_post_tag_post_count( $post_tag ) {
$term = get_term_by( 'slug', $post_tag, 'post_tag' );
if ( ! $term ) {
return;
}
// You get back an WP_Term object
// You can then use $term->count, which gives you the
// number of posts attached to this post tag.
echo (int) $term->count;
}
The property $term->count gives you the number of posts that are attached to that term.
You use it like this:
render_post_tag_post_count( 'apartamenty' );
What if I want to get it by post tag ID?
Take the above code and change the first argument from slug to id. Then you would pass the post tag's ID instead of its slug.
You can learn more about the parameters in WordPress Codex.

order results by category, wordpress

I have a category page called 'features' this is all fine, but the posts in the features category also belong to a certain genre of film, and it's this what I want to order the posts by on the actual features category template.
eg
features cat template brings in all features posts.
then what I want to do is display in alpha order by whatever genre it also belongs to
features
action
post
post
post
comedy
post
post
sci-fi
post
post
etc.
this is what I have at the moment ( the cat numbers relate to the genres = action=10 etc)
query_posts('cat=10,11,12,13,14,15,16,17,18&orderby=title&order=DESC' );
while (have_posts()) : the_post();
How can I list all the posts (group them) by genre ? when I use title here i guess it's using the posts title.
Playing around if I stick this in the post loop
foreach((get_the_category()) as $childcat) {
if (cat_is_ancestor_of(4, $childcat)) {
echo $childcat->cat_name;
}
}
this returns the actual genre cat for each post while in the loop, but I'm not sure how to stick it together so I can state the ordering of the posts by groups of genre, I was hoping I could do this in the query_posts?
Any help in the right direction would be greatly appreciated.
I would consider first doing a query for your sub categories, and then loop through your sub categories querying the posts for each. Like so:
$categories = get_categories( array ( 'child_of' => 10, 'orderby' => 'name' ) );
foreach( $categories as $category ){
// query_posts for category by $category->term_id
// Display posts for this category
}
Does that work for what you are wanting to do?

Categories