WordPress.org

Ready to get started?Download WordPress

Forums

Custom function to display 30 random published posts (3 posts)

  1. su1
    Member
    Posted 1 year ago #

    Hello,

    I need a custom function to display 30 random published posts in a post (a sort of random sitemap but for only 30 posts).

    I've succeeded to code that by changing a small piece of code found on the internet, but as this code was made to create a chronological sitemap originally, I think the final result is much bigger than what I actually need. This is my code, it's working fine, but how can make it shorter and more efficient from there without breaking it?

    function sitemap_f( $atts ) {
    
    	global $month, $wpdb, $wp_version;
    
    	// a mysql query to get the list of distinct years and months that posts have been created
    	$sql = 'SELECT
    			DISTINCT YEAR(post_date) AS year,
    			MONTH(post_date) AS month,
    			count(ID) as posts
    		FROM ' . $wpdb->posts . '
    		WHERE post_status="publish"
    
    		GROUP BY YEAR(post_date),
    			MONTH(post_date)
    		ORDER BY RAND()';
    
    	// use get_results to do a query directly on the database
    	$archiveSummary = $wpdb->get_results($sql);
    	$posts_affiches=0;
    	$casse=0;
    	$nombre_posts = 500;
    	// if there are any posts
    	if ($archiveSummary) {
    		// loop through the posts
    		foreach ($archiveSummary as $date) {
    			// reset the query variable
    			unset ($bmWp);
    			// create a new query variable for the current month and year combination
    			$bmWp = new WP_Query('year=' . $date->year . '&monthnum=' . zeroise($date->month, 2) . '&posts_per_page=-1');
    
    			// if there are any posts for that month display them
    			if ($bmWp->have_posts()) {
    				// display the archives heading
    				$url = get_month_link($date->year, $date->month);
    				$text = $month[zeroise($date->month, 2)] . ' ' . $date->year;
    
    				echo get_archives_link($url, $text, '', '<h3>', '</h3>');
    				echo '<ul class="postspermonth">';
    
    				// display an unordered list of posts for the current month
    				while ($bmWp->have_posts()) {
    					$bmWp->the_post();
    					echo '<li><a href="' . get_permalink($bmWp->post) . '" title="' . wp_specialchars($text, 1) . '">' . wptexturize($bmWp->post->post_title) . '</a></li>';
    					$posts_affiches += 1;
    					if ($posts_affiches == $nombre_posts) {
    						$casse = 1;
    						break;
    					}
    				}
    
    				if ($casse == 1) {
    					break;
    				}
    
    				echo '</ul>';
    			}
    		}
    	}
    
    }
    add_shortcode( 'sitemap', 'sitemap_f' );
  2. keesiemeijer
    moderator
    Posted 1 year ago #

    Do you need it as a shortcode?
    See this example on how to Display a list of 5 random posts.
    http://codex.wordpress.org/Template_Tags/get_posts#Random_posts

  3. su1
    Member
    Posted 1 year ago #

    I need to display the sitemap inside a post so I think shortcodes are the way to go?

    I tried to adapt the example you gave me to a custom function:

    function sitemap_f( $atts ) {
    
    echo "<ul>";
    $args = array( 'numberposts' => 5, 'orderby' => 'rand' );
    $rand_posts = get_posts( $args );
    foreach( $rand_posts as $post ) :
    	echo '<li><a href="'; the_permalink(); echo '">'; the_title(); echo '</a></li>';
    endforeach;
    echo "</ul>";
    
    }
    add_shortcode( 'sitemap', 'sitemap_f' );

    but it always return the same post in the sitemap (the post which is calling the function). Can this code be run inside the loop? I think no, that's why I was using a direct query to the database.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags