Seen a lot of posts elsewhere, but nothing quite hitting the mark.
I need to echo the description of a specific term of a custom taxonomy related to a custom post type.
Right now, I'm doing the following loop successfully to pull content for related custom posts per term (in this case my custom post type is 'rental_gear' and my term is 'cameras':
<? $args = array(
'post_type'=> 'rental_gear',
'type' => 'cameras',
'order' => 'ASC'
);
$the_query = new WP_Query( $args );
if($the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();
?>
<li>› <? the_title(); ?></li>
<?php endwhile; endif; wp_reset_postdata(); ?>
Can I pull the term's description in this loop?
I've tried the 'get_term' function and pulling '$term->description;' but those are related to the post, and this is on an overview page listing information from many posts (hence the use of the loop)
You didn't mention the name of your custom taxonomy, so replace your-taxonomy. This will query ALL rental_gear posts that have the camera term in your-taxonomy.
<?php
$args = array(
'post_type' => 'rental_gear',
'tax_query' => array(
array(
'taxonomy' => 'your-taxonomy',
'field' => 'slug',
'terms' => 'camera'
)
)
);
$query = new WP_Query( $args );
Related
I want to auto assign some tags to a post category
I am using the wp set post tags function in the functions.php file.
wp_set_post_tags( $post_id, 'tag1,tag2', true );
It works when I put in the post id number. I need help with looping through the posts from a category.
Hope someone can help!
Thanks
You need to write query and then apply the function for it. Let me share coding for this.
For custom post type and taxonomy the following code will work. Make sure to replace post_type , taxonomy and terms according to your value.
$args = array(
'post_type' => 'custom_post_type',
'tax_query' => array(
array(
'taxonomy' => 'custom_taxonomy_name',
'field' => 'term_id',
'terms' => array(1,2,3),
),
),
);
$the_query = new WP_Query($args);
For post type post there the query is simple
$args = array(
'post_type' => 'post',
'category__in' => array(11,12,13)
);
$the_query = new WP_Query( $args );
Make sure to replace category__in with category id you want to run the query.
After that please run the loop
<?php
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
wp_set_post_tags( $the_query->post->ID, 'tag1,tag2', true );
}
} else {
// No post found
}
/* Restore original Post Data */
wp_reset_postdata();
?>
I have a question related to WordPress. I have made a dropdown of custom taxonomies on single.php page That's the code I am using to list my taxonomies:
<ul class="dropdown-menu" aria-labelledby="dLabel">
<?php $terms = get_terms( array(
'taxonomy' => 'subject',
'hide_empty' => false,
) );
if( !empty( $terms ) ):
foreach ($terms as $key => $value) :
$term = $value->name;
?>
<li><?php echo $term; ?></li>
<?php endforeach; endif; wp_reset_query(); ?>
</ul>
Right now all taxonomy in dropdown have the same post link that is opend on the page. I want that every taxonomy should have the link of it's respective first post and when I click on a taxonomy it open that taxonomy's first post on the same single.php page. Is there any way of doing that?
Here is the picture of my page:
That's how my taxonomy dropdown look like if you zoom and look in the bottom left corner you will see the current post link that is showing on all subject names is there any way that I can change this link to that subject's first post link. Right now biology taxonomy post is open when I click on chemistry first post of chemistry open on this page. Please help me in doing this task I'll be very thankful to you.
After having the term-object, you can lookup the first post and get its permalink. in my example i ordered by date desc. Then set the link of the post:
<ul class="dropdown-menu" aria-labelledby="dLabel">
<?php
$terms = get_terms(array(
'taxonomy' => 'subject',
'hide_empty' => false,
));
if (!empty($terms)):
foreach ($terms as $key => $value) :
$term = $value->name;
//get the first post of this term **
$args = array(
'post_type' => 'post',
'orderby' => 'date',
'order' => 'DESC',
'numberposts' => 1,
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'subject',
'field' => 'id',
'terms' => $value->term_id,
)
)
);
$posts=get_posts($args);
$first_page_link = get_permalink($posts[0]->ID);
//**
?>
<li><?= $term ?></li>
<?php endforeach;
endif;
wp_reset_query(); ?>
</ul>
<?php
The code is not tested. And of course, you should do some checks, like what to do when no post is available with that term.
I have custom post types with select option.In select option I got all the taxonomy categories and on change of select it displayes all posts of post type but when I go to single page it displayes only this current post not all from this category. I tried this code
$term = $wp_query->queried_object; but it displayes only the current post of this category not all.
How can I get all posts from the current taxonomy category.
Seems I got the solution.Here is my final code
$terms = wp_get_post_terms( $post->ID, 'course_type' );
if($terms){
// post has course_type terms attached
$course_terms = array();
foreach ($terms as $term){
$course_terms[] = $term->slug;
}
$original_query = $wp_query;
$wp_query = null;
$wp_query = new WP_Query( array(
'post_type' => 'courses',
'tax_query' => array(
array(
'taxonomy' => 'course_type',
'field' => 'slug',
'terms' => $course_terms, //the taxonomy terms I'd like to dynamically query
'posts_per_page' => '-1'
),
),
'orderby' => 'title',
'order' => 'ASC'
) );
if ( have_posts() ): ?>
<ul>
<?php while (have_posts() ) : the_post(); ?>
<li><? php the_title(); ?></li>
<?php endwhile; ?>
</ul>
<?php endif;
$wp_query = null;
$wp_query = $original_query;
wp_reset_postdata();
} // end if($terms)
I have a simple query_posts using a custom post type and a taxonomy linked to it. I use the plugin MAGIC FIELD to achieve my goal.
Here is the code.
<?php
// Display the persons with Sport Injuries Speciality //
wp_reset_query();
// Args
$args = array(
'post_type' => 'physician',
'order' => 'ASC',
'orderby' => 'menu_order',
'type_of_speciality' => 'Sport Medecine specialist'
);
// The Query
query_posts( $args );
// The Loop
while ( have_posts() ) : the_post();
result here
endwhile;
wp_reset_query();
?>
As you can see, I want to display only the posts with 'Sport Medecine specialist' as there speciality. The admin has to check a Radio button.
Is there something I'm missing because I should only have 3 results and it gives me all the posts for this custom post type.
EDIT #1:
I have made a little work around, but this is not really good for speed optimization since it loop in all the custom post types.
<?php
// Display the persons with Sport Injuries Speciality //
wp_reset_query();
// Args
$args = array(
'post_type' => 'physician',
'order' => 'ASC',
'orderby' => 'menu_order',
);
// The Query
query_posts( $args );
// The Loop
while ( have_posts() ) : the_post();
if(get('type_of_speciality') == 'Sport Medecine specialist')
{
echo the_title();
}
endwhile;
wp_reset_query();
?>
As you can see, the if() condition check if the "taxonomy" has Sport Medecine specialist as value and exit(else) if the value do not match.
Like I said, it is not very good because if I would have 1000 physicians, it would loop trought the 1000's.
Any idea to light me up?
Change the way you're querying it, instead of tax => value, use tax_query and instead of using query_posts() use WP_Query
See if it works for you.
WP_Query taxonomy parameters
<?php
$args = array(
'post_type' => 'physician',
'order' => 'ASC',
'orderby' => 'menu_order',
'tax_query' => array(
array(
'taxonomy' => 'type_of_speciality',
'field' => 'slug', //term_id or slug
'terms' => 'sport-medecine-specialist', // the term ( id or slug ) based on the value of field above
),
),
);
$search = new WP_Query( $args );
while ( $search->have_posts() ) : $search->the_post();
the_title();
endwhile;
wp_reset_postdata();
Trying to list all Custom Post Type titles based on filtered Custom Taxonomy Terms I am getting the list of all post titles instead of getting the list of Queried post. Here is the code I am using:
<?php
$loop = new WP_Query(
array(
'post_type' => 'photos',
'technique' => 'zevar',
'post_child' => 0,
'posts_per_page' => 10
)
);
?>
<?php
while ( $loop->have_posts() ) : $loop->the_post();
?>
<?php the_title(); ?>
<?php endwhile; ?>
As you can see I have a Custom Post Type called "photos" and Custom Taxonomy registered as "technique". Under Taxonomy "technique" I have some terms which one of them is "zevar". Can you please let me know what I am doing wrong here?
Your taxonomy query is not correct. check following code.
<?php
$loop = new WP_Query(
array(
'post_type' => 'photos',
'tax_query' => array(
array(
'taxonomy' => 'technique',
'field' => 'slug',
'terms' => 'zevar'
)
),
'posts_per_page' => 10
)
);
?>
<?php
while ( $loop->have_posts() ) : $loop->the_post();
?>
<?php the_title(); ?>
<?php endwhile; ?>
Reference on how to use taxonomy parameter https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters