I want archives.php to be able to show multiple custom taxonomies at once.
Right now this works if I write either
domain.com/?taxonomy=red&taxonomy2=circle
or
domain.com/?taxonomy=red+blue
But how do I make my list of taxonomies link correctly to this? Right now I'm only able to get domain.com/?taxonomy=red and not add anything else to the URL.
The sidebar on archives.php contains the following code:
<?php
//First Query for Posts matching term1
$custom_terms = get_terms('taxonomy');
foreach($custom_terms as $custom_term) {
wp_reset_query();
$args = array('post_type' => 'taxonomy',
'tax_query' => array(
array(
'taxonomy' => 'taxonomy',
'field' => 'slug',
'terms' => $custom_term->slug,
'orderby' => 'title'
),
),
);
$loop = new WP_Query($args);
if($loop->have_posts()) {
echo '<li>'.$custom_term- >name.'';
echo '</li>';
}
}
?>
Related
please help im new in web development i dont know what to put on fields and ids i already had custom post type. i want to show the custom post type on normal post related topic its specific post...
<?php
//get the taxonomy terms of custom post type
$customTaxonomyTerms = wp_get_object_terms( $post->ID, 'your_taxonomy', array('fields' => 'ids') );
//query arguments
$args = array(
'post_type' => 'profile',
'post_status' => 'publish',
'posts_per_page' => 5,
'orderby' => 'rand',
'tax_query' => array(
array(
'taxonomy' => 'profile',
'field' => 'id',
'terms' => $customTaxonomyTerms
)
),
'post__not_in' => array ($post->ID),
);
//the query
$relatedPosts = new WP_Query( $args );
//loop through query
if($relatedPosts->have_posts()){
echo '<ul>';
while($relatedPosts->have_posts()){
$relatedPosts->the_post();
?>
<li><?php the_title(); ?></li>
<?php
}
echo '</ul>';
}else{
//no posts found
}
//restore original post data
wp_reset_postdata();
?>
I have this query based on WordPress docs, but no result. If I remove the tax_query it gives all publish posts, but when I add it, it displays no result.
I have checked the database and I'm sure there is post attached to the wp_term_taxonomy so I'm not sure why it doesn't display results.
What I am doing here, I get all custom taxonomy type then loop through it to get all posts related to it.
$event_terms = get_terms(
'event_type',
array(
'orderby'=>'name',
'hide_empty'=>true
)
);
if(!empty($event_terms) && !is_wp_error($event_terms)){
foreach($event_terms as $event_term){
// code for each event term
$args = array(
'status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'event_type',
'field' => 'slug',
'terms' => $event_term->slug,
),
),
);
$loop = new WP_Query($args);
if($loop->have_posts()) {
echo '<h2>'.$event_term->name.'</h2>';
while($loop->have_posts()) : $loop->the_post();
echo ''.get_the_title().'<br>';
endwhile;
}
// this displays all taxonomy terms
echo $event_term->name."-".$event_term->term_taxonomy_id."<br>";
}
}
Please change your text query like this and try:
$the_query = new WP_Query( array(
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'event_type',
'field' => 'slug',
'terms' => $event_term->slug
)
)
) );
$count = $the_query->found_posts;
I hope this is helps you.
I have created some taxonomies and post_types trough custom post type UI plugin.
I pass taxonomy_id with a form to a page and I receive it correctly [var_dump($_POST)] shows me number example 30. I want to show posts from that custom post type category: I tried the code below but it returns nothing.
$args = [
'tax_query' => array(
array(
'taxonomy' => 'school_type',
'field' => 'term_id',
'terms' => array('30','22'),
// 'operator' => 'IN'
),
'post_type' => 'school',
)
];
if($q->have_posts()):
while($q->have_posts()){
$q->the_post();
echo the_title();
}
else:
echo 'nothing';
endif;
Can anyone help me?
You should have a $arg variable like this and then use the WP_Query() object to get your posts.
$args = array(
'post_type' => 'school',
'posts_per_page'=>30,
'post_status' => 'publish',
'tax_query' => array(array(
'taxonomy' => 'school_type',
'field' => 'term_id',
'terms' => array('30','22'),
);
$q = new WP_Query($args);
if($q->have_posts()):
while($q->have_posts()){
$q->the_post();
echo the_title();
}
else:
echo 'nothing';
endif;
Put $q = new WP_Query( $args ); before your if line
change your permalink to post type and update permalinks, hope this will help.
You have the post_type in the tax_query array, plus you're targeting $q when it doesn't exist (not that I can see. anyway).
Try something like this :-
$args = array(
'post_type' => 'school',
'tax_query' => array(
array(
'taxonomy' => 'school_type',
'field' => 'term_id',
'terms' => array('30','22'),
),
),
'numberposts' => -1
);
$q = new WP_Query($args);
if($q->have_posts()):
while($q->have_posts()){
$q->the_post();
echo the_title();
}
else:
echo 'nothing';
endif;
I want to display a list of posts that have a certain tag but don't have another specific tag. For examples, I have tried the following to display a list of posts which have 'animal' tag.
<?php
$args = array(
'numberposts' => 1,
'tag' => 'animal',
'showposts' => 8
);
$query = new WP_Query($args);
if($query->have_posts()):
echo '<table>';
while($query->have_posts()): $query->the_post();
the_title(); ?> </a></td>
endwhile;
endif;
wp_reset_query();
?>
How do we display a list of posts in 'animal' tag but not in 'cat' tag for example?
I am very new with wordpress and just learned to create the custom page.
You are going to have to make use of a tax_query here to make this work. The normal tag parameters won't do the job.
Just a few notes on your original code
showposts is depreciated in favor of posts_per_page
numberposts is not valid in WP_Query
Use wp_reset_postdata() for WP_Query, wp_reset_query() is used with query_posts which should never ever be used
You need to call wp_reset_postdata() before endif just after endwhile
You will need something like this
$args = array(
'posts_per_page' => '8',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'post_tag',
'field' => 'slug', //Can use 'name' if you need to pass the name to 'terms
'terms' => 'animal', //Use the slug of the tag
),
array(
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => 'cat',
'operator' => 'NOT IN',
),
),
);
$query = new WP_Query( $args );
You could use the tag__not_in param (see here) but you would need the term_id of the cat tag. So maybe something like this:
<?php
// get term_id of unwanted cat tag for tag__not_in param
$tag = get_term_by('name', 'cat', 'post_tag');
$args = array(
'numberposts' => 1,
'tag_slug__in' => array('animal'),
'tag__not_in' => array($tag->term_id),
'showposts' => 8
);
$query = new WP_Query($args);
if($query->have_posts()):
echo '<table>';
while($query->have_posts()): $query->the_post();
the_title(); ?> </a></td>
endwhile;
endif;
wp_reset_query();
?>
With use of the Advanced Custom Fields plugin I created a select dropdown which contains 6 membership types. All of my 'listings' using this custom field are assigned one of the 6.
I have managed to get my listings to display in order of membership level, however it's not defining by category that your currently in. It's grabbing listings from every other category too.
<?php
// args
$args = array(
'numberposts' => -1,
'post_type' => 'directory_listings',
'meta_key' => 'membership_type',
'orderby' => 'meta_value',
'taxonomy' => 'listing_category'
);
// query
$wp_query = new WP_Query( $args )
?>
<?php if (have_posts()) : ?>
<?php
while( $wp_query->have_posts() ) {
the_post();
ldl_get_template_part('listing', 'compact');
ldl_get_featured_posts();
}
?>
<?php else : ?>
<?php endif; ?>
Also, I am using the plugin: https://wordpress.org/plugins/ldd-directory-lite/
WP_Query does not have a taxonomy parameter, you should use tax_query instead. More info in the Codex.
'tax_query' => array(
array(
'taxonomy' => 'listing_category',
'field' => 'slug',
'terms' => 'my-listing-category',
),
),
To grab the current taxonomy term dynamically (assuming you're on a listing_category taxonomy page):
'tax_query' => array(
array(
'taxonomy' => 'listing_category',
'field' => 'term_id',
'terms' => get_queried_object_id(),
),
),