• I want to make a link towards random image attachment of a post (say having post id =1).

    Now we already have his excellency Matt written a plug in for similar purpose.

    We just need to modify the query part…

    Following is the code of random redirect

    function matt_random_redirect() {
    	global $wpdb;
    
    	$query = "SELECT ID FROM $wpdb->posts WHERE post_type = 'post' AND post_password = '' AND 	post_status = 'publish' ORDER BY RAND() LIMIT 1";
    
    	if ( isset( $_GET['random_cat_id'] ) ) {
    		$random_cat_id = (int) $_GET['random_cat_id'];
    		$query = "SELECT DISTINCT ID FROM $wpdb->posts AS p INNER JOIN $wpdb->term_relationships AS tr ON (p.ID = tr.object_id AND tr.term_taxonomy_id = $random_cat_id) INNER JOIN  $wpdb->term_taxonomy AS tt ON(tr.term_taxonomy_id = tt.term_taxonomy_id AND taxonomy = 'category') WHERE post_type = 'post' AND post_password = '' AND 	post_status = 'publish' ORDER BY RAND() LIMIT 1";
    	}
    
    	if ( isset( $_GET['random_post_type'] ) ) {
    		$post_type = preg_replace( '|[^a-z]|i', '', $_GET['random_post_type'] );
    		$query = "SELECT ID FROM $wpdb->posts WHERE post_type = '$post_type' AND post_password = '' AND 	post_status = 'publish' ORDER BY RAND() LIMIT 1";
    	}
    
    	$random_id = $wpdb->get_var( $query );
    
    	wp_redirect( get_permalink( $random_id ) );
    	exit;
    }
    
    if ( isset( $_GET['random'] ) )
    	add_action( 'template_redirect', 'matt_random_redirect' );
    
    ?>

    Can any one help me in finding what would be the query for it?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter umair

    (@umair)

    any one
    ?
    Matt, you?? 😉

    Thread Starter umair

    (@umair)

    hello?

    let me get this straight: you have (for example) 5 images attached to a post.
    And now you want to provide a LINK (as in “Text-Link”) to a random image of those 5?

    In that case I suggest you do some searching for “post image” “latest image” and similar for a function, and set the “order by” to “rand()”.

    Thread Starter umair

    (@umair)

    @mores:
    Yes, you have correctly understand.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Link towards random image attachement’ is closed to new replies.