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;
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've got a custom post type that I've sorted by date from an ACF date picker field. I need to be able to filter these results based on a custom taxonomy that I've got. I can't seem to work out where to add the tax_query array. Keeps breaking the site.
Taxonomy name is 'pre_job_status', term to filter by is 'survey-complete'
Currently I have the following code
<?php
// get posts
$posts = get_posts(
'tax_query' => array(
array(
'taxonomy' => 'pre_job_status',
'field' => 'slug',
'terms' => array( 'survey-complete' )
),
),
array(
'post_type' => 'pre_jobs',
'posts_per_page' => -1,
'meta_key' => 'survey_date',
'orderby' => 'meta_value',
'order' => 'ASC',
));
if( $posts ): ?>
<hr>
<ul>
<?php foreach( $posts as $post ):
setup_postdata( $post )
?>
<?php $requestor = get_field('pre_job_requestor', $client->ID ); ?>
<?php $survey_site = get_field('survey_site', $client->ID ); ?>
<li>
<?php the_field('survey_date'); ?> - <?php the_title(); ?> - <?php echo $requestor[0]->post_title; ?> - <?php echo $survey_site[0]->post_title; ?>
</li><hr>
<?php endforeach; ?>
</ul>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
Your get_posts() query should look like the following:
// get posts
$posts = get_posts(array(
'post_type' => 'pre_jobs',
'posts_per_page' => -1,
'meta_key' => 'survey_date',
'orderby' => 'meta_value',
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'pre_job_status',
'field' => 'slug',
'terms' => array( 'survey-complete' )
),
),
));
The tax_query array should be added to the main query array.
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 been trying to list my custom type 'movies' with the custom taxonomy genre with a value of drama.
I have tried several solutions but as yet I have failed.
<?php
$args = array(
'post_type' => 'movies',
'tax_query' => array(
array(
'taxonomy' => 'genre',
'field' => 'term_id',
'terms' => 'drama'
)
)
);
$drama = new WP_Query( $args );
if ( $drama->have_posts() ) : while ( $drama->have_posts() ) : $drama->the_post(); ?>
<h4><?php echo the_title(); ?></h4>
<?php endwhile; ?>
<?php else : echo '<p>NO CONTENT FOUND</p>'; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
Hope someone can shed some light on this matter.
Steven
The field parameter is wrong, in your case you need to set it to slug as drama is not a term_id
$args = array(
'post_type' => 'movies',
'tax_query' => array(
array(
'taxonomy' => 'genre',
'field' => 'slug',
'terms' => 'drama'
)
)
);
Hope it works !
Following is my wordpress query which I am using to display all the posts but the query is not displaying the post's tags, Also kindly let me know how to modify the following query so it displays the posts from any specific category.
<?php
$wp_query2 = null;
$wp_query2 = new WP_Query(array(
'post_type' => 'post',
'post_status' => 'publish',
'caller_get_posts'=> 0 ));
while ($wp_query2->have_posts()) : $wp_query2->the_post();
?>
<?php the_date(); ?>
<br />
<?php the_title(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
<?php
wp_reset_query();
?>
You may try this
$args = array(
'post_type' => 'post',
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => array( 'category1', 'category2' ) // replace these
),
array(
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => array( 'tag1, tag2' ) // replace these
)
)
);
$query = new WP_Query( $args );
while ($query->have_posts()) : $query->the_post();
// ...
the_content();
the_tags(); // display tags
endwhile;
Check WP Query and the_tags.