Ive been trying to figure this out for three days now, even using solutions on this site. I still cant get this working.
I have a wordpress loop that uses a filter to show posts by post type. Now the post type is called "case-studies" Thus all the posts in the type case studies are shown.
But i need to hide a specific taxonomy term from this loop. The taxonomy is called "sectors" and the term is "healthcare". Ive tried all manner of combinations but still cant get this. I need this pretty urgent. Anyone who can help would save my life.
Here is the query and the loop
<?php
// The Query
$the_query = new WP_Query( 'post_type=case-studies&posts_per_page=-1' );
// The Loop
while ( $the_query->have_posts() ) :
$the_query->the_post();
?>
$args = array(
'post_type' => 'case-studies',
'tax_query' => array(
array(
'taxonomy' => 'sectors',
'field' => 'slug',
'terms' => array('comercial', 'personal', 'etc') //excluding the term you dont want.
)
)
);
$query = new WP_Query( $args );
I dind´t try it but you could just make a query calling only the terms you want, you could previously populate the terms array listing all the terms on the taxonomy and excluding the one you want, i think this is a little hacky it should be another straight forward way to do it but give it a try since it is a case of life or dead =).
source: http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters
Try this one:
<?php
$type = 'cpreviews';
$args=array(
'post_type' => $type,
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<p><?php the_title(); ?></p>
<?php
endwhile;
}
wp_reset_query(); // Restore global post data stomped by the_post().
?>
ok then try this one it will extract your terms dinamicaly and exclude the term you dont want, i didn´t check if it is working but this is the logic, please check for sintax errors.
$terms = get_terms("sectors");
$count = count($terms);
$termsAr = array();
if ($count > 0 ){
foreach ( $terms as $term ) {
if($term->name !== "healthcare"){//Here we exclude the term or terms we dont want to show
array_push($termsAr, $term->name);
}
}
}
$terms = get_terms("types");
$count = count($terms);
$termsAr2 = array();
if ($count > 0 ){
foreach ( $terms as $term ) {
if($term->name !== "healthcare"){//Here we exclude the term or terms we dont want to show
array_push($termsAr2, $term->name);
}
}
}
$args = array(
'post_type' => 'case-studies',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'sectors',
'field' => 'slug',
'terms' => $termsAr //excluding the term you dont want.
),
array(
'taxonomy' => 'types',
'field' => 'slug',
'terms' => $termsAr2 //excluding the term you dont want.
)
)
);
$query = new WP_Query( $args );
Related
after several unsuccessful searches, I ask my question here.
Indeed, I'm trying to display a list of posts grouped by categories:
CAT A
post1
post2
post3
CAT B
post4
post5
post6
post7
...
Here is the code I tried.
I can display the categories, but not the posts
<?php
$terms = get_terms( 'secteur', array(
'orderby' => 'count',
'hide_empty' => 0
) );
foreach( $terms as $term ) {
$args = array(
'post_type' => 'client',
'posts_per_page' => '-1',
'secteur' => $term->slug
);
$query = new WP_Query( $args );
echo'<h3>' . $term->name . '</h3>';
// Start the Loop
while ( $query->have_posts() ) : $query->the_post();
$secteur_dactivite = get_field( 'secteur_dactivite' );
echo '<div class="cat-'.esc_html( $secteur_dactivite->slug ). '"><img src="'.get_field( 'logo' ).'"></div>';
endwhile;
wp_reset_postdata();
}
?>
You need to use tax_query as an WP query attribute, instead of secteur.
Try replacing that with:
$args = array(
'post_type' => 'client',
'posts_per_page' => '-1',
'tax_query' => array(
array(
'taxonomy' => 'secteur',
'field' => 'slug',
'terms' => $term -> slug,
),
),
);
thank you very much for your response.
Unfortunately, this does not change the display. The titles are displayed but not the articles.
<h3>CAT1</h3
<h3>CAT2</h3>
<h3>CAT3</h3>
If it helps, I can display all the items with the following code :
<?php
$posts = get_posts(array(
'numberposts' => -1,
'post_type' => 'client',
'post_status' => 'publish'
));
if($posts)
{
echo '<div class="row all-item">';
foreach($posts as $post)
{
echo '<div"><img src="'.get_field( 'logo' ).'"></div>';
}
echo '</div>';
}
?>
Thank you very much for your answers.
But nothing has worked for me, and I can’t come up with any solutions.
I think the problem lies in the configuration of my taxonomy.
This is my custom post type configuration (client):
https://imgur.com/uDom3PH
my taxonomy configuration (secteur) :
https://imgur.com/WRwsSbR
my custom field (secteur_dactivite) :
https://imgur.com/NKQ4GPn
Thanks again for your help
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 show related posts with the same tag at the bottom of a post, but I also want to add one random post, that has not the tag, like "try this completely different thing".
I've tried with tag__not_in, but my code doesn't work:
$tag_id = get_queried_object()->term_id;
$args = [
'post__not_in' => array( get_queried_object_id() ),
'tag__not_in' => array( $tag_id ),
'posts_per_page' => 1,
'orderby' => 'rand'
];
$my_query = new wp_query( $args );
if( $my_query->have_posts() ) {
while( $my_query->have_posts() ) {
$my_query->the_post(); ?>
Sorry, I don't really know how to code, I just try to understand Wordpress' code and how I can modify it.
How do I get the posts tag id into the array?
Update> This works:
global $post;
$posttags = get_the_tags();
if ($posttags) {
foreach($posttags as $tag) {
// just the test echo $tag->slug;
$tag = get_term_by('name', $tag->slug, 'post_tag');
$args = array(
'posts_per_page' => 1,
'tag__not_in' => array($tag->term_id),
'orderby' => 'rand',
);
}
}
$query = new WP_Query($args);
What's the error you're getting?
Regardless, I don't think you need 'post__not_in' because the 'tag__not_in' will take care of that for you.
After that, I'd look at the $tag_id = get_queried_object()->term_id; and make sure you're assigning the value you want to that variable. Echo $tag_id somewhere and confirm it's what you want.
Finally, I think you need your new wp_query( $args ); to be new WP_Query( $args );
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();
I'm trying to query posts based on a custom field, and then display them in a loop. I've checked and double checked my code against the codex and other sources, but the query still does not appear to be working. What am I doing wrong?
Stripped down to the essentials, my code looks like this:
<?php
$args = array(
'meta_key' => 'my_custom_field'
);
$my_query = new WP_Query( $args );
?>
<?php if ( $my_query->have_posts() ) { ?>
<p>Success, we have posts!!!</p>
<?php } else { ?>
<p>Uh Oh, No posts!!!</p>
<?php } ?>
The conditional statement is dropping through and returning "Uh Oh, no posts".
I've checked the postmeta table, and there are definitely posts that contain the meta_key _my_custom_field. I have tried the query both with and without leading underscore.
What am I doing wrong?
I use this for search a date between two custom dates field in my custom post type "porfolio", i think that you are in a similar situation:
$args = array(
'post_type' => 'portfolio',
'posts_per_page' => '10',
'meta_query' => array(
array('key' => 'portfolio_start_date', 'value' => data_to_db2($ricerca_data), 'compare' => '<=', 'type' => 'NUMERIC'),
array('key' => 'portfolio_end_date', 'value' => data_to_db2($ricerca_data), 'compare' => '>=', 'type' => 'NUMERIC')
)
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
$post_count = wp_count_posts();
while ( $the_query->have_posts() ) {
// DO WHAT YOU WANT
}
}
My advice is to use meta_query in $args array