I`ve found sth like this: Is it possible to create a shortcode that will query a post based on taxonomies
function posts_shortcode_handler($atts, $content) {
extract(shortcode_atts(array(
'posts_per_page' => '5',
'post_type' => 'gallery'
), $atts));
global $post;
$temp = $post;
$posts = new WP_Query($atts);
$retVal = '';
if ($posts->have_posts()) {
while ($posts->have_posts()) {
$posts->the_post();
// these arguments will be available from inside $content
$parameters = array(
'PERMALINK' => get_permalink(),
'TITLE' => get_the_title(),
'CONTENT' => get_the_content(),
'CATEGORIES' => get_the_category_list(', '),
'THUMBNAIL' => get_the_post_thumbnail()
);
$finds = $replaces = array();
foreach ($parameters as $find => $replace) {
$finds[] = '{' . $find . '}';
$replaces[] = $replace;
}
$retVal .= str_replace($finds, $replaces, $content);
}
}
wp_reset_query();
$post = $temp;
return $retVal;
}
add_shortcode('galerie', 'posts_shortcode_handler');
My shortcode looks like this:
[galerie post_type="gallery" posts_per_page="5" taxonomy_name="movies"]
<h5><a href="{PERMALINK}">{TITLE}</a></h5>
<div>{THUMBNAIL}
{CONTENT}</div>
[/galerie]
My problem is about the taxonomy_name="movies" that`s not working for me. In my custom taxonomy name 'Kategorie' I have two sub categories 'movies' and 'photos'. Shortcode ignores choosen 'taxonomy_name' and display all custom posts from post_type="gallery". I would like to choose sub category in my custom_taxonomy to display custom post type from shortcode.
Please Help me, I`m stuck :(
You have to replace "taxonomy_name" with the name of your taxonomy. The first line of your shortcode should look like this:
[galerie post_type="gallery" posts_per_page="5" Kategorie="movies"]
That way, this is the $atts array passed to WP_Query:
$args = array(
'post_type' => 'post',
'posts_per_page' => '5',
'kategorie' => 'movies'
);
More details here: http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters