WordPress.org

Forums

Help with random post - only with images (2 posts)

  1. soelver
    Member
    Posted 5 years ago #

    With help from different sites I've googled, I've come up with this (which doesn't work):

    <center>
    <br /><br />
    <?php
    $rand_posts = $wpdb->get_results("SELECT
    FROM $wpdb->posts.*
    LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_ID)
    WHERE $wpdb->post.post_status = 'publish'
    AND $wpdb->post.post_type = 'post'
    AND $wpdb->postmeta.meta_key = 'images'
    ORDER BY RAND() LIMIT 5");
    foreach($rand_posts as $post){
    setup_postdata($post);
    ?>
    <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php if(function_exists('show_one_image')) {show_one_image(get_post_meta($post->ID, 'images', true)); } ?></a>
    
    <? }
    ?>
    <br /><br />

    I've tried to follow the method used here:
    Query based on Custom Field and Category

    When the code was like this, it worked:

    <center>
    <br /><br />
    <?php
    $rand_posts = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_status = 'publish' AND post_type='post' ORDER BY RAND() LIMIT 5");
    foreach($rand_posts as $post){
    setup_postdata($post);
    ?>
    <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php if(function_exists('show_one_image')) {show_one_image(get_post_meta($post->ID, 'images', true)); } ?></a>
    
    <? }
    ?>
    
    <br /><br />

    I changed it to include a left join, as I need to show ONLY post with images in. The images is uploaded through a script, and shows up in the postmeta table, in column "Meta_Key", and the field says image, when an image is uploaded with the post.

    Right now, it doesn't show anything at all. The site is still up, and no errormessages occurs.

    It may be really obvious, and I must say, that php is not my thing - at all. So I'm hoping for a little help from someone who knows about left joins in WordPress.

    Thanks in advance :)

  2. soelver
    Member
    Posted 5 years ago #

    Found the edit-button, to remove the way my link in previous post was all over the text ;)

    Learning while living.

Topic Closed

This topic has been closed to new replies.

About this Topic