Support » Plugin: Related Posts by Taxonomy » Display all posts randomly

  • Hello there,

    I simply want to display other posts within the post. But all posts, not only posts from the Category in which the current post is.
    I thought this [related_posts_by_tax related=”false”] shortcode does that, but it still displays posts only from the category from which the current post is.

    Any shortcode to display ALL posts, including other categories? Like 5 random posts?

    Thanks

Viewing 1 replies (of 1 total)
  • Plugin Author keesiemeijer

    (@keesiemeijer)

    moderator

    Hi blaivas

    If related is set to false it displays related posts by post date, instead of by terms in common. But they are still related by the terms.

    You could get your own related posts with this filter
    https://keesiemeijer.wordpress.com/related-posts-by-taxonomy/filters/#related_posts_by_taxonomy_pre_related_posts

    Put this in your functions.php file.

    
    add_filter( 'related_posts_by_taxonomy_pre_related_posts', 'rpbt_override_related_posts', 10, 2 );
    
    function rpbt_override_related_posts( $related_posts, $args ) {
    	// Use widget or shortcode settings for our own query.
    	$post_args = array(
    		'post_type' => $args['post_types'],
    		'posts_per_page' => $args['posts_per_page'],
    		'orderby' => 'rand',
    	);
    
    	// Format thumbnails needs a meta query for posts with post thumnails.
    	if ( 'thumbnails' === $args['format'] ) {
    		$post_args['meta_query'] = array(
    			array(
    				'key' => '_thumbnail_id',
    			),
    		);
    	}
    
    	$my_related_posts = get_posts( $post_args );
    
    	return $my_related_posts;
    }

    This will get random posts with the WordPress function get_post()
    https://developer.wordpress.org/reference/functions/get_posts/

    I’m sure there are plugins that do this out of the box for you.
    https://wordpress.org/plugins/display-posts-shortcode/

Viewing 1 replies (of 1 total)
  • You must be logged in to reply to this topic.