Get images from gallery in taxonomy - php

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.

Related

How to display posts of taxonomy in WordPress page template?

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 );

retrieving and display images by tag in wordpress

i iam creating a section with tags with this code, is a function to retrieve tags and exclude some tags also,
$args = array('name__like' => $name_like, 'exclude' => array(75,177,42,74,197,36,40,140,162,108,86,47,4,29,22,215,87,151,104),'order' => 'ASC');
$tags = get_tags( $args );
if ( !empty( $tags ) && !is_wp_error( $tags ) ) {
$count = count($tags);
$i=0;?>
<ul class="my_term-archive">
<?php
foreach ($tags as $tag) {
$i++;
$tag_link = get_tag_link( $tag->term_id );
$tag_id = get_tag_ID($tag->name);
if(strtolower(substr($tag->name,0,1)) !=$name_like){
continue;
}
//i need a function here to retrieve images with the id of the tag
//attached
//////
$html .= "<li><a href='{$tag_link}' id='{$tag_id}' title='{$tag->name} Tag' class='{$tag->slug}'>";
$html .= "{$tag->name}</a></li>";
}
}
echo $html;
?>
</ul>
then i put this code in my functions.php file in wordpress, to make avaliable the tag box in the picture managment, so i can tag pictures now,
function wptp_add_tags_to_attachments() {
register_taxonomy_for_object_type( 'post_tag', 'attachment' );
}
add_action( 'init' , 'wptp_add_tags_to_attachments' );
so my question is how can find and display the images by the id tag ?
sorry my bad english, is not my native lenguage. any help is very welcome. thanks
You can actually handle this with a basic WP_Query call. There's lots of details and options for the WP_Query object here, but I'd think you could do something like this:
$args = array(
'post_type' => 'attachment',
'tax_query' => array(
array(
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => 'whatever-your-tag-slug-is',
),
),
);
$query = new WP_Query( $args );

Counting posts in category and auto removing from category

my posts are listed in 2 categories. (featured, news).
iam trying to count posts in 'featured' category, and if its more than 5 i want it to remove 6,7,8, ...
only keep latest 5..
so far iam able to count them using this code
$category = get_category(830);
$count = $category->category_count;
if( $count > 5 ) {
// stuff
}
but inside the if statement how to get items 6,7,8... ?
i just want to remove category "featured" with category_id 830, and keep other category (news) if its listed in it.
i made this function , but i get error 500 when i use it.
can you help me?
$my_query = new WP_Query( 'category_name=featured' );
function countfeatures($my_query) {
$featurecount = 0;
while ( $my_query->have_posts() ) : $my_query->the_post();
$featurecount++;
if ($featurecount > 5){
$pos = array_search( 'featured', $post_cats );
unset( $post_cats[$pos] );
wp_set_post_terms ($post_id, $post_cats, 'category');
//wp_set_post_terms ($slide->ID, $post_cats, 'category');
//do stuff
}
endwhile;
}
add_filter('pre_get_posts', 'countfeatures');
Use Limit in query for 5 and Order by Primary key descending,so You will get top five latest record
Only orderby and order parameter needs to be added this will get you
newest 5 posts of featured category.
$args = array(
'post_type' => 'post',
'post_status'=> 'publish',
'posts_per_page'=>5,
'order'=>'DESC',
'orderby'=>'ID',
'tax_query' => array(
array(
'taxonomy' => 'featured',
'field' => 'term_id',
'terms' => array(830),
'operator' => 'IN',
),
),
);
$query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();

WP Query to get posts with specific term_taxonomy

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

How To Display Categories and the posts inside of a Custom Post Type

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;
}
?>

Categories