WordPress shortcodes for terms - wordpress

I was trying to create shortcode for custom taxonomy terms dynamically. But failing to do so.
Suppose, if there is a term called "wordpress" then I should be able to query all the posts associated with that term via shortcode.
To be more precise, suppose if there is a taxonomy called 'event' and under that taxonomy there are multiple terms. So, I was trying to query posts under each of the term via shortcode of each of the term.
Here is what I tried:
function wordpress_recent_post( $atts, $content ) {
$a = shortcode_atts( array(
'cat' => '',
), $atts );
$args = array(
'posts_per_page' => 1,
'offset' => 0,
'category_name' => $a['cat'],
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'ignore_sticky_posts' => true,
);
$recent_posts = new WP_Query( $args );
ob_start();
if ( ! $recent_posts-> have_posts() ) {
return 'No Posts Found for ' . $a['cat'];
}
while ( $recent_posts->have_posts() ) {
$recent_posts->the_post();
the_title( '<h2>', '</h2>' );
if ( '' != $a['cat'] ) {
$href = '/category/' . $a['cat'];
} else {
$href = '/blog';
}
echo "<p><a href='$href'>Read More" . ucwords( $a['cat'] ) . '</a></p>';
}
wp_reset_query();
return ob_get_clean();
}
add_shortcode( 'wordpress_recent_post', array($this, 'wordpress_recent_post') );
And then I used this to call the posts from a term called "features" whose id is '183' (suppose)
[wordpress_recent_post cat="183"]
Any help would be really very appreciable.
Thanks!

Adding term slug did it. There should be slug not id, like this:
[wordpress_recent_post cat="features"]

Related

How to show post in it's specific category, not parent

I have a parent category which has a lot of child categories, which also have a lot of their child categories and these categories have posts. I want to show on page all the child categories titles from the first parent category and posts titles only once.
Now I have code which shows posts several times after every loop iteration, but I want to display it only once.
Here is my code snippet
# get child categories
$sub_cats = get_categories( array(
'child_of' => $parent_id,
'hide_empty' => 0
) );
if( $sub_cats ){
foreach( $sub_cats as $cat ){
echo '<h3>'. $cat->name .'</h3>';
# get posts from category
$myposts = get_posts( array(
'numberposts' => -1,
'category' => $cat->cat_ID,
'orderby' => 'post_date',
'order' => 'DESC',
) );
# show posts
global $post;
foreach($myposts as $post){
setup_postdata($post);
echo '<li class = "test">'. get_the_title() .'</li>';
}
}
wp_reset_postdata();
}
I'm not WordPress developer, but I need to do this task. I didn't write this code, I just found it on the internet.
Can someone help me to show posts only once or improve this code?
thank you
If you have duplicate post outputted:
It's certainly because post has multiple categories. So a single post will be found in multiple categories.
To prevent it, you could make a query for unique posts from your categories:
see:
//get terms from taxonomy category
$sub_cats = get_categories( [
'child_of' => $parent_id,
'hide_empty' => 0
] );
//wp-query post from your categories
$query = new WP_Query( [ 'category__in' => array_map( function ( $o ) {
return $o->term_id;
}, $sub_cats ) ] );
//only unique - never tested this
add_filter('posts_distinct', function(){return "DISTINCT";});
//your posts
$posts = $query->posts;
If you need to keep your loop as it is, you can use an array to store displayed post_ids and prevent duplicate to show:
# get child categories
$sub_cats = get_categories( array(
'child_of' => $parent_id,
'hide_empty' => 0
) );
$sub_cats_unique_posts = []; //array to store displayed post IDs
if( $sub_cats ){
foreach( $sub_cats as $cat ){
echo '<h3>'. $cat->name .'</h3>';
# get posts from category
$myposts = get_posts( array(
'numberposts' => -1,
'category' => $cat->cat_ID,
'orderby' => 'post_date',
'order' => 'DESC',
) );
# show posts
global $post;
foreach($myposts as $post){
if(!in_array($post->ID, $sub_cats_unique_posts)){ //if not displayed already
setup_postdata($post);
echo '<li class = "test">'. get_the_title() .'</li>';
$sub_cats_unique_posts[] = $post->ID; //add this post ID to displayed one
}
}
}
wp_reset_postdata();
}
This should only display unique posts from $parent_id child categories.

Wordpress - Query comments by post meta

I am using the code below (simplified) to display a list of the last 10 comments:
<?php
$args = array(
'post_type' => 'tarefa',
'number' => '10',
'order' => 'DESC',
'orderby' => 'comment_date',
//'meta_key' => 'field_name',
//'meta_value' => 'field_value',
);
$comments_query = new WP_Comment_Query;
$comments = $comments_query->query( $args );
foreach ( $comments as $comment ) {
echo '<p>';
echo get_the_title($comment->comment_post_ID) . '<br>'; //post title
echo $comment->comment_content; // comment content
echo '</p>';
};
?>
Question:
Well, meta_key and meta_value seem to be associated to comment_meta... But in my case, I have to display comments based on post_meta key and value.
Any sugestions?
You can try this code.
You need to add a query for posts to get array of post ides with meta key.
Then use that array into comments query argument.
//QUERY FOR POSTS WITH META KEY AND VALUE (META QUERY)
$post_args = array(
'post_type' => 'post',
'meta_key' => 'meta key',//Meta key of post
'meta_value' => 'meta value',//String or Numeric value
'meta_compare' => '=',
);
$post_query = new WP_Query( $post_args );
$posts_array= array();
if ( $post_query->have_posts() ) {
while ( $post_query->have_posts() ) {
$post_query->the_post();
$posts_array[] = get_the_ID(); //Array of post ids
}
wp_reset_postdata();
}
//YOUR COMMENT ARGS SHOULD BE THIS
$args = array(
'post_type' => 'tarefa',
'number' => '10',
'order' => 'DESC',
'orderby' => 'comment_date',
'post__in' => $posts_array, //THIS IS THE ARRAY OF POST IDS WITH META QUERY
);
Try this , then let me know the result.
My first question here at Stackoverflow and that worked perfectly.
Thank you very much, Souvik!
Below the final result (simplified):
$post_args = array(
'post_type' => 'tarefa',
'posts_per_page' => -1,
'meta_key' => 'field_name',
'meta_value' => 'field_value',
);
$post_query = new WP_Query( $post_args );
$posts_array= array();
if ( $post_query->have_posts() ) {
while ( $post_query->have_posts() ) {
$post_query->the_post();
$posts_array[] = get_the_ID(); //Array of post ids
}
wp_reset_postdata();
}
//YOUR COMMENT ARGS SHOULD BE THIS
$args = array(
'number' => '30',
'order' => 'DESC',
'orderby' => 'comment_date',
'post__in' => $posts_array, //THIS IS THE ARRAY OF POST IDS WITH META QUERY
);
$comments_query = new WP_Comment_Query;
$comments = $comments_query->query( $args );
foreach ( $comments as $comment ) {
echo '<p>';
echo get_the_title($comment->comment_post_ID) . '<br>'; //post title
echo $comment->comment_content; // comment content
echo '</p>';
};

Need example code for wp_playlist_shortcode

I trying to use wp_playlist_shortcode for creating playlist with audio-files from all blog posts.
In official documentation i saw this parameter:
'ids'
(array) Create a playlist out of these explicit attachment IDs. If empty, a playlist will be created from all $type attachments of $id. Default empty.
I trying this code, and its doesn't working:
$attch_id = array('76', '73', '70', '67');
wp_playlist_shortcode( array( 'ids' => '$attch_id' );
How to create playlist with audio-files from all blog posts? Now i use this code, but it is a not playlist.
$audios = array( 'post_type' => 'attachment', 'posts_per_page' => -1, 'post_status' => null, 'post_parent' => null, 'post_mime_type' => 'audio/mpeg' );
$attachments = get_posts( $audios );
if ($attachments) {
foreach ( $attachments as $post ) {
setup_postdata($post);
$media_url = $post->guid;
$media_title = $post->post_title;
echo wp_audio_shortcode( array( 'src' => $media_url) );
echo '<p>' . $media_title . '</p>';
// print_r($media_url);
}
}
wp_reset_postdata();

Display Custom Post Type w/ Custom Taxonomy Using Shortcode

I am trying to display a custom post type (testimonials) along with a specific taxonomy term using a shortcode. Right now the shortcode is displaying all posts from the testimonials custom post type instead of the targeted taxonomy term.
This is the shortcode I think I'm meant to use:
[list-testimonials terms="37"]
In order to execute the following PHP:
// create shortcode with parameters so that the user can define what's queried
add_shortcode( 'list-testimonials', 'post_listing_parameters_shortcode' );
function post_listing_parameters_shortcode( $atts ) {
ob_start();
// define attributes and their defaults
extract( shortcode_atts( array (
'type' => 'testimonials',
'order' => 'date',
'orderby' => 'title',
'posts' => -1,
'taxonomy' => 'testimonial-category',
), $atts ) );
// define query parameters based on attributes
$options = array(
'post_type' => $type,
'order' => $order,
'orderby' => $orderby,
'posts_per_page' => $posts,
'taxonomy_name' => $terms,
);
$query = new WP_Query( $options );
if ( $query->have_posts() ) { ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
**THE HTML OUTOUT**
<?php endwhile;
wp_reset_postdata(); ?>
<?php $myvariable = ob_get_clean();
return $myvariable;
}
}
I think I'm really close. What am I missing here? Any help is greatly appreciated. Thanks!

Adding custom fields to the search and results

I'm using the plugin "WP Search Suggest" to suggest in the search field.
Now, the plugin works good, but it only suggest and search titles.
I looked in the plugin and found this:
$query_args = apply_filters(
'wpss_search_query_args',
array(
's' => $s,
'post_status' => 'publish',
'post_type' => 'movie',
),
$s
);
$query = new WP_Query( $query_args );
if ( $query->posts ) {
foreach ( $query->posts as $post ) {
$results[] = $post->post_title;
}
$results = apply_filters(
'wpss_search_results',
$results,
$query
);
echo join( $results, "\n" );
}
and I tried to add support to custom field by adding:
'meta_key' => 'somthing',
'meta_value' => '$s'
but I didn't work, and didn't found a way to 'echo' the results because the '$query->posts' array don't hold the meta keys or values...

Resources