In my wordpress installation, I have a custom taxonomy event-categories which is mapped to custom post type event.
In my single post display page, I need to list all posts which is posted in same event-categories of the current post. How can I write a wp query for this?
Screen-shot for my custom taxonomy in custom post.
Now tried like this get_the_terms(the_ID(), 'event-categories').
So I got all term_taxonomy_ids related to the single post. Next how can I get all posts which have these term_taxonomy_id.
This would be about the most basic query to solve your problem.
$term_tax_ids = get_the_terms(get_the_ID(), 'event-categories');
$terms = array();
foreach($term_tax_ids as $term_tax_id) {
array_push($terms, $term_tax_id->term_id);
}
$args = array(
'post_type' => 'event',
'posts_per_page' => 5,
'tax_query' => array(
array(
'taxonomy' => 'event-categories',
'field' => 'id',
'terms' => $terms,
'operator' => 'IN',
)
)
);
$query = new WP_Query( $args );
while ( $query->have_posts() ) {
$query->the_post();
echo '<div class="related_single">' . get_the_post_thumbnail();
echo '<div class="related_title">' . get_the_title() . '</div></div>';
}
You should really read the Codex, it has literally everything you could ever want to know about queries
Related
Let's say I have a website which sells cars.
I'm using a custom taxonomy called brand for the manufacturer (like BMW, Audi, ...) and a custom taxonomy called type for the type of cars (like SUV, Coupe, ...).
For the cars itself I'm using a custom post type called models.
Now I want to show every car brand in the taxonomy archive for type (All brands with SUVs).
To do that, I'm trying to get all brands and filter them with all types.
As result there should be a list with all car brands that have SUVs.
Here's my current code to get a list of brands:
$taxonomies = get_terms( array(
'taxonomy' => 'brand',
'hide_empty' => false
) );
if ( !empty($taxonomies) ) :
$output = '<select>';
foreach( $taxonomies as $category ) {
if( $category->parent == 0 ) {
$output.= '<optgroup label="'. esc_attr( $category->name ) .'"></optgroup>';
}
}
$output.='</select>';
echo $output;
endif;
I couldn't find a way to add a second taxonomy to this snippet.
Is this the wrong approach?
Maybe I need to get the custom post types (models) first to check which one has both terms?
This article has a really helpful breakdown.
I needed to get the taxonomies that were part of another taxonomy for use in creating a dropdown (and some other uses.) This code allowed me to identify the "categories" that were part of a particular "destination".
$dest_slug = get_query_var('term');
$desination_ids = get_posts(array(
'post_type' => 'item',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'destinations',
'field' => 'slug',
'terms' => $dest_slug
)
),
'fields' => 'ids'
));
$categories = wp_get_object_terms($desination_ids, 'category');
I found a solution that works:
$productcat_id = get_queried_object_id();
$args = array(
'numberposts' => -1,
'post_type' => array('models'),
'tax_query' => array(
array(
'taxonomy' => 'brand',
'field' => 'term_id',
'terms' => $productcat_id,
),
),
);
$cat_posts = get_posts($args);
$my_post_ids = wp_list_pluck ($cat_posts, 'ID');
$my_terms = wp_get_object_terms ($my_post_ids, 'types');
if ( !empty($my_terms)) :
echo '<ul>';
foreach( $my_terms as $my_term ):
$brand_name = $my_term->name;
$brand_link = get_term_link($my_term);
echo '<li><a alt="'.$brand_name.'" href="'.esc_url( $brand_link ).'">'.$brand_name.'</a></li>';
endforeach;
echo '</ul>';
endif;
Not sure if this has been asked before, but I'm a bit lost. I've created a "Newsroom" Pods with a custom taxonomy of Newsroom Category. Newsroom Category has 3 fields: Press Release, Media, Others. I have a WordPress page template: taxonomy-newsroom_category.php
taxonomy-newsroom_category.php is used to display Pods posts if meets the following:
1 - pods = 'newsroom'
2 - taxonomy = 'press_release' || 'media' || 'others'
My issue right now is that I can't find a way to display the post details:
image(thumbnail), title(post title), date_published
I hope someone can help. Thanks
Here's the code I'm currently using:
<?php
//Setup Pod object
//Presuming permalink structure of example.com/pod-name/item-name
//See http://pods.io/code/pods/find
//Set $params to get 5 items
$params = array(
'limit' => 5,
);
//get current pod name
$pod_name = pods_v( 0, 'newsroom');
//get pods object
$pods = pods( $pod_name, $params );
//check that total values (given limit) returned is greater than zero
if ( $pods->total() > 0 ) {
//loop through items using pods::fetch
while ($pods->fetch() ) {
//Put title/ permalink into variables
$post_title = $pods->display('post_title');
$date_published = $pods->display('date_published');
$permalink = site_url( trailingslashit( $pod_name ) . $pods->field('permalink') );
?>
<div class="news-item col-sm-4">
<div class="news-item-img"></div>
<div class="news-item-header">
<h5 class="news-category"></h5>
<h2 class="news-item-title"><?php echo $post_title; ?></h2>
<h5 class="news-item-date"><?php echo $date_published; ?></h5>
</div>
</div><!-- close -->
<?php
} //endwhile;
} //endif;
// Output Pagination
//see http://pods.io/docs/code/pods/pagination
echo $pods->pagination( );
?>
I also found out solution for that... I had my Custom Post Type "studies" and I had to filter them, based on custom taxonomy category. If you want to write Posts of one taxonomy category, try to use something like that:
$type = $_GET['type'];
$args = array(
"post_type" => "studien",
"post_per_page" => -1,
"relation" => "AND"
);
if($type != "") {
$args['tax_query'][] = array(
'taxonomy' => 'market',
'field' => 'slug',
'terms' => $type
);
$wp_query = new WP_Query($args);
}
$type represents one category in my created Taxonomy (value comes from HTML code in select option), $args Is some query on database and 'market' is slug of my custom Taxonomy, $wp_query returns all filter Posts
screenshot of my custom taxonomy in custom post type. As you can see, I have two groups. First is clicked in two Posts and Second is clicked in last two Posts. Maybe it will helps you to give imagination
Check this out https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters. This document explained how to query with custom post type and taxonomy in details. Youd code could be something like this.
$args = array(
'post_type' => 'newsroom',
'tax_query' => array(
array(
'taxonomy' => 'newsroom_category',
'field' => 'slug',
'terms' => 'press_release',
),
),
);
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
$id = get_the_ID(); // with post id, you can get whatever you want.
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
/* Restore original Post Data */
wp_reset_postdata();
} else {
// no posts found
}
from the wordpress documenation:
https://codex.wordpress.org/Custom_Taxonomies
we have the below , for taxonomy person
$args = array(
'tax_query' => array(
array(
'taxonomy' => 'person',
'field' => 'slug',
'terms' => 'bob'
)
)
);
$query = new WP_Query( $args );
I have a custom post type with multiple attributes within it,
such as price
color make model, etc.
Which is would like Wordpress to print the amount of posts within them.
my current code is this
// Get total number of posts in "vehiclestock" post type
$count_vehiclestock = wp_count_posts('listings');
$total_vehiclestock = $count_vehiclestock->publish;
echo $total_vehiclestock . ' listings. ';
I've attached some examples to better explain myself.
as I am unaware of what to search for to answer my own question
Add This Code for count all post or category wise count post
$args = array(
'posts_per_page' => -1,
'post_type'=> 'post',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => 'category_slug'
)
),
);
$wp_query = new WP_Query( $args );
$post_count = $wp_query->post_count;
I need to filter a custom post type based on a users selection. Eg the user chooses a category from a input select and the list of posts dynamically updates.
How do I go about achieving this?
Here is my WP_Query to load the posts
<?php
$args = array(
'post_type' => 'careers',
);
$posts = new WP_Query($args);
if($posts->have_posts()) :
while($posts->have_posts()) :
$posts->the_post();
echo '<div class="otherrow"><div class="otherpostion"><h3>',
the_field('position'),
'</h3><p>',
the_field('brief'),
'</p></div><div class="othercontract"><h3>',
the_field('contract'),
'</h3></div><div class="otherdate"></h3>',
the_date('d / m / y', '<h3>', '</h3>'),
'</h3></div><div class="otherlink"><h3><a href="',
the_permalink(),
'" >VIEW JOB</a></h3></div></div> ';
endwhile;
endif;
?>
The Category I would like to filter is 'locations', eg "location1' and 'location2'. I have already set this up and have pages only showing single locations but the home page needs to let the user filter the list.
Any help would be greatly appreciated.
Please use wp_query with custom Taxonomy parameter on dropdown change event :
$args = array(
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'people',
'field' => 'slug',
'terms' => 'bob',
),
),
);
$query = new WP_Query( $args );
Hi I have created my own custom post type within Wordpress to contain projects that i can call via my theme files.
I am new to creating my own themes. I currently am using the following code in my single.php file to call in related articles based on the category of the blog post.
<?php
// Default arguments
$args = array(
'posts_per_page' => 3, // How many items to display
'post__not_in' => array( get_the_ID() ), // Exclude current post
'no_found_rows' => true, // We don't ned pagination so this speeds up the query
);
// Check for current post category and add tax_query to the query arguments
$cats = wp_get_post_terms( get_the_ID(), 'category' );
$cats_ids = array();
foreach( $cats as $wpex_related_cat ) {
$cats_ids[] = $wpex_related_cat->term_id;
}
if ( ! empty( $cats_ids ) ) {
$args['category__in'] = $cats_ids;
}
// Query posts
$wpex_query = new wp_query( $args );
// Loop through posts
foreach( $wpex_query->posts as $post ) : setup_postdata( $post ); ?>
<div class="col-md-4 related-post">
<?php the_post_thumbnail('large'); ?>
<?php the_title(); ?>
</div>
<?php
// End loop
endforeach;
// Reset post data
wp_reset_postdata(); ?>
In my new post type "projects" i would like to call in related projects. Which im assuming would be very similar code except i need to stop it looking for posts and instead look for my projects.
Here is my code for new post type:
// Projects
add_action( 'init', 'create_post_type' );
add_post_type_support( 'bw_projects', 'thumbnail' );
add_post_type_support( 'bw_projects', 'custom-fields' );
function create_post_type() {
register_post_type( 'bw_projects',
array(
'labels' => array(
'name' => __( 'Projects' ),
'singular_name' => __( 'Projects' )
),
'public' => true,
'has_archive' => true,
'taxonomies' => array( 'category','post_tag'),
)
);
}
What would i need to change in my first code snippet in order to look for bw_projects and not look for 'posts' anymore. I tried playing around and changing certain lines myself but i caused more issues and stopped the page loading. Is this even right i can use the same code, slightly altered or would i need something completely different?
Thanks in advance.
You can get any post type that you require using get_posts();
<?php $args = array(
'posts_per_page' => 5,
'offset' => 0,
'category' => '',
'category_name' => '',
'orderby' => 'date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'projects',
'post_mime_type' => '',
'post_parent' => '',
'author' => '',
'author_name' => '',
'post_status' => 'publish',
'suppress_filters' => true
);
$posts_array = get_posts( $args ); ?>
Simply set the 'post_type' argument to that of you custom post type, to get only these posts. You can also set the number of post, and filter by category etc.
You can find more info in the codex.
Alternatively, if you wanted to keep something similar to your existing code you could try using 'pre_get_posts' to filter the query to just your projects. However you'd need to remember to add / remove this filter so it only operates on the queries that need it.
To display the posts you can use a simple foreach to churn them out. You#d obviously want to do some sort of styling to get the layout correct:
$args = array("posts_per_page" => 10, "orderby" => "comment_count");
$posts_array = get_posts($args);
foreach($posts_array as $post)
{
echo "<h1>" . $post->post_title . "</h1><br>";
echo "<p>" . $post->post_content . "</p><br>";
}
Or a really concise way of doing all of the above would be something like:
$args = array("posts_per_page" => 5, "post_type" => "projects");
$posts_array = get_posts($args);
foreach($posts_array as $post)
{
echo "<h1>" . $post->post_title . "</h1><br>";
echo "<p>" . $post->post_content . "</p><br>";
}