0

I have a short-code in my wordpress site in file functions.php I have this short-code

function Insert_NewsBlogsByTags($attr) {
    $a = shortcode_atts(array(
        'title' => 'News',
        'selector' => '#all',
        'number' => 4,
        'tags' => ''
    ), $attr);
    ob_start();
    the_widget(
        'Fbstax_Widget_Recent_Posts_By_Categories_Tags',
        array(
            'title' => esc_html__($a['title'], 'fbstax-theme'),
            'selector' => esc_html__($a['selector'], 'fbstax-theme'),
            'number' => $a['number'],
            'tags' => $a['tags']
        )
    );
    return ob_get_clean();
}
add_shortcode( 'InsertNewsBlogsByTags', 'Insert_NewsBlogsByTags' );

Here I am calling for a widget to display posts by some tags. I am making selection in widget class file like this:

 $r = new WP_Query(apply_filters('widget_posts_args', array(
            'posts_per_page' => $number,
            'no_found_rows' => true,
            'post_status' => 'publish',
            'ignore_sticky_posts' => true,
            'cat' => '20,45',
            'tag_id' => $instance['tags']
        ), $instance ) );

But when I call in it in page builder like this:

[InsertNewsBlogsByTags tags="70, 244"]

It doesn't display anything. Only one tag can be selected. Can you advise how to fix it?

5
  • Try to check the number of parameters in the array Commented Apr 25, 2018 at 9:51
  • stackoverflow.com/questions/31307306/… Commented Apr 25, 2018 at 9:51
  • I have added $no_whitespaces = preg_replace( '/\s*,\s*/', ',', filter_var( $a['tags'], FILTER_SANITIZE_STRING ) ); $tags_array = explode( ',', $no_whitespaces ); but it doesn't help Commented Apr 25, 2018 at 10:06
  • Try using the tag__in parameter: In the widget class, in the WP_Query call, replace 'tag_id' => $instance['tags'] with 'tag__in' => wp_parse_id_list( $instance['tags'] ) Commented Apr 25, 2018 at 10:55
  • 2
    it works? thanks a lot Commented Apr 25, 2018 at 11:59

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.