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><a href="'.esc_url( home_url( '/catphoto/' ) ).$uniquePar.'/?mark='.$uniqueSlugs[$var].'">'.$uniqueTerms[$var].'</a></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