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 );
Related
I have two custom post types, 'product' and 'product_review'. The CPT 'product_review' has a taxonomy 'product_name' whose slug matches the CPT 'product'
An example 'Product A' is the product post. 'Product Review A' is the product_review post that has a 'product_name' taxonomy value of 'product_a'.
I need to show how many 'product_review' each 'product' has.
<?php
global $post;
$post_slug = $post->post_name;
//echo $post_slug;
//this outputs the name of the product, which I need
$terms = get_terms('product_review');
foreach ( $terms as $post_slug ) {
echo $term->count . 'Reviews';
?>
It doesn't show any count. I want it to show how many $terms(how many reviews) are tied to $post_slug(name of product). The 'product_review' slug matches the slug of the product.
You can use a custom WP_Query and the found_posts prop like below:
// example query args
$args = [
// look for reviews cpt
'post_type' => 'product_review',
// limit to posts that match our taxonomy product name
'tax_query' => [
[
'taxonomy' => 'product_name',
'field' => 'slug',
'terms' => [ 'product_a' ]
]
]
];
// run the query
$query = new WP_Query( $args );
// grab "total" found posts
$total = $query->found_posts;
// display the total
echo $total
// reset post data so we dont break the main loop
wp_reset_postdata();
#mikerojas answer got me close, but wasn't returning accurate data. Here is what I came up with that got me what I needed.
<?php
$review_args = array(
'post_type' => 'my_post_type',
'post_status' => 'publish',
'tax_query' => array(
array (
'taxonomy' => 'my_taxonomy',
'field' => 'slug',
//this is already inside a loop, making it dynamic to only grab the reviews whose tax value equals the post slut
'terms' => $post->post_name,
)
),
);
if($num = count( get_posts ( $review_args)) <= 1) {
echo 'Review ' . $num = count( get_posts( $review_args ) );
} else {
echo 'Reviews ' . $num = count( get_posts( $review_args ) );
}
wp_reset_postdata();
?>
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 );
There seems to be several answers to similar questions but I have not yet found one working for me.
I have a custom post-type called entertainement. entertainement has a taxonomy called ent_categories.
One of the ent_categories is called Event
Each Event has a gallery and I am trying to make a query that will return the latest 10 images that has been added to any of the CPT entertainment with the category Event.
Im hoping to receive a list of urls in an array.
From what I read here something like this should do the trick:
$arg = array(
'post_status' => 'inherit',
'posts_per_page' => -1,
'post_type' => 'attachment',
);
$arg['tax_query'] = array(
array(
'taxonomy' => 'ent_categories',
'field' => 'name',
'terms' => array( 'Event' ),
),
);
$the_query = new WP_Query( $arg );
var_dump($the_query);
The var_dump($the_query); displays a lot of things but no images?
Any tips on this one?
Thank you
EDIT:
I just saw that I can do this:
function pw_show_gallery_image_urls( $content ) {
global $post;
// Only do this on singular items
if( ! is_singular() )
return $content;
// Make sure the post has a gallery in it
if( ! has_shortcode( $post->post_content, 'gallery' ) )
return $content;
// Retrieve all galleries of this post
$galleries = get_post_galleries_images( $post );
$image_list = '<ul>';
// Loop through all galleries found
foreach( $galleries as $gallery ) {
// Loop through each image in each gallery
foreach( $gallery as $image ) {
$image_list .= '<li>' . $image . '</li>';
}
}
$image_list .= '</ul>';
// Append our image list to the content of our post
$content .= $image_list;
return $content;
}
add_filter( 'the_content', 'pw_show_gallery_image_urls' );
This result in that all the gallery images urls get displayed below the images in the gallery.
Maybe this function could be called from a page instead than from functions.php?
You were on the right track, but you're querying for posts of type attachment with a term of the ent_categories taxonomy which only applies to entertainement posts, so there won't be any of them as you'll see if you:
var_dump($the_query->posts);
If you dump all $the_query you'll see lots of things because it's a WP_Query object.
You need to query your entertainement posts: (Be careful because you have a typo in your slug!)
$arg = array(
'posts_per_page' => -1,
'post_type' => 'entertainement',
);
$arg['tax_query'] = array(
array(
'taxonomy' => 'ent_categories',
'field' => 'name',
'terms' => 'Event',
),
);
$the_query = new WP_Query( $arg );
Then you can iterate the posts and get the gallery items like this:
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
if (get_post_gallery()) :
echo get_post_gallery();
print_r(get_post_gallery_images());
endif;
}
/* Restore original Post Data */
wp_reset_postdata();
}
get_post_gallery_images() will get you an array of the URL's of gallery images
get_post_gallery() will get you actual HTML to print the gallery.
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
I need some help here as I've exhausted every place I can trying to find information. This is what I'm trying to do:
I have created a custom Post type in my admin called "Classes"
That works fine, the data works great and it's inputting in the admin.
I want to make a custom template to show this custom post type. However, everything I try it's not displaying properly. I've tried many code variations.
I know someones already done this and has the block of code to display this. This is what I need the code to do:
List All categories in my custom post type 'classes'
List all posts (show all content, not a link or excerpt) inside of each category.
Display it as such (I'm using Jquery Accordion)
the_category()
the_title()
the_content()
========================================================
By the way, Here is the block of code I'm currently using. It does work, but it ONLY shows the posts, all of them. It does not show the category with posts inside of them.
<?php
$type = 'classes';
$args = array (
'post_type' => $type,
'post_status' => 'publish',
'paged' => $paged,
'posts_per_page' => 10,
'ignore_sticky_posts'=> 1
);
$temp = $wp_query; // assign ordinal query to temp variable for later use
$wp_query = null;
$wp_query = new WP_Query($args);
if ( $wp_query->have_posts() ) :
while ( $wp_query->have_posts() ) : $wp_query->the_post();
echo '<h3 class="acc1">';
the_title();
echo '</h3>';
echo '<div class="sc"><div class="vs">View Schedule</div>';
the_content();
echo '</div>';
endwhile;
else :
echo '<h2>Not Found</h2>';
get_search_form();
endif;
$wp_query = $temp;
?>
Community, I need you. Please give your feedback!
What you want to do is actually start with a category query. You have to make sure you query all your categories with your custom post type:
Then for each category you would do pretty much what you have above.
$taxonomy = 'classes';
$args = array('hide_empty' => false,);
$terms = get_terms( $taxonomy, $args );
foreach($terms as $val) {
$term_id = $val->term_id;
$term_name = $val->name;
// now do post query
}
You most likely would have to display the category name as a header for your accordion as well.
Here's all the args for get_terms:
http://codex.wordpress.org/Function_Reference/get_terms
For that query you also most likely have to use a Simple Taxonomy Query (search for that on the page).
http://codex.wordpress.org/Class_Reference/WP_Query
By adding this arg to your above query:
'tax_query' =>
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => array( $term_name )
)
Is that what you were looking for?
There might be a better way to do this but I just had to recently do this and did pretty much what I just outlined here.
I should have been more clear and said to put the posts query within the foreach of the terms query.
Here's the updated answer based on your last reply (I have not tested this).
<?php
$taxonomy = 'classes';
$args = array('hide_empty' => false,);
$terms = get_terms( $taxonomy, $args );
foreach($terms as $val) {
$term_id = $val->term_id;
$term_name = $val->name;
$type = 'classes';
$args = array (
'post_type' => $type,
'post_status' => 'publish',
'paged' => $paged,
'posts_per_page' => 10,
'ignore_sticky_posts'=> 1,
'tax_query' =>
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => array( $term_name )
)
);
$temp = $wp_query; // assign ordinal query to temp variable for later use
$wp_query = null;
$wp_query = new WP_Query($args);
if ( $wp_query->have_posts() ) :
while ( $wp_query->have_posts() ) : $wp_query->the_post();
echo '<h3 class="acc1">';
the_title();
echo '</h3>';
echo '<div class="sc"><div class="vs">View Schedule</div>';
the_content();
echo '</div>';
endwhile;
else :
echo '<h2>Not Found</h2>';
get_search_form();
endif;
$wp_query = $temp;
}
?>