WordPress.org

Ready to get started?Download WordPress

Forums

Random Redirect for Attachments of a post. (4 posts)

  1. umair
    Member
    Posted 5 years ago #

    I want to use matt's random redirect plugin ...
    but not for posts, i want to use it for picture attachments of that post?

    Following is the if condition Matt is using, how can it be modified for attachment of a single post( which say have post id = 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;`

  2. umair
    Member
    Posted 5 years ago #

    any one ?

  3. umair
    Member
    Posted 5 years ago #

    hello........

  4. wp_guy
    Member
    Posted 5 years ago #

    You can add this bit:

    if ( isset( $_GET['random_attachment'] ) ) {
    		$post_parent = $_GET['random_attachment'];
    		$query = "SELECT ID FROM $wpdb->posts WHERE post_type = 'attachment' AND post_parent = '$post_parent' ORDER BY RAND() LIMIT 1";
    	}

    Then get it like this: yourblog.com/?random&random_attachment=1 (for post ID 1)

    Hope that helps

Topic Closed

This topic has been closed to new replies.

About this Topic