WordPress.org

Ready to get started?Download WordPress

Forums

Link towards random image attachement (5 posts)

  1. umair
    Member
    Posted 5 years ago #

    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?

  2. umair
    Member
    Posted 5 years ago #

    any one
    ?
    Matt, you?? ;)

  3. umair
    Member
    Posted 5 years ago #

    hello?

  4. mores
    Member
    Posted 5 years ago #

    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()".

  5. umair
    Member
    Posted 5 years ago #

    @mores:
    Yes, you have correctly understand.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.