• Hi there

    I’m still pretty new at more complex WordPress stuff so any help would be most appreciated.

    I’ve been using google and the search feature on www.remarpro.com to find something to help me but unfortunately haven’t found what I’m looking for.

    I’m using the following code within the loop of a post:

    <?php
     $querystr = "
        SELECT $wpdb->posts.*
        FROM $wpdb->posts, $wpdb->postmeta
        WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id
        AND $wpdb->postmeta.meta_key = 'type'
        AND $wpdb->postmeta.meta_value = 'personal'
        AND $wpdb->posts.post_status = 'publish'
        AND $wpdb->posts.post_type = 'post'
        AND $wpdb->posts.post_date < NOW()
        AND $wpdb->posts.ID NOT IN ($post->ID)
        ORDER BY rand()
        LIMIT 3
     ";
     $pageposts = $wpdb->get_results($querystr, OBJECT);
     ?>

    to generate 3 random posts according to the filters within the code. It’s working perfectly like this, but I want to take it one step further…

    Basically what I need to know is how would I go about getting this line:

    AND $wpdb->postmeta.meta_value = 'personal'

    to get the value of the custom field ‘type’ for the particular post ($post_id) which the query is working with.

    I was thinking something along the lines of:

    AND $wpdb->postmeta.meta_value = get_post_meta($post_id, 'type', $single)

    I’ve tried many combinations in my trial-and-error attempts to make this work – and they either result in a page error or not displaying any posts on the page. I’m also not sure if I’m supposed to create a function outside of the query and then call it into the query?

    Anybody who can show me the correct code to use would be educating me greatly (I’m still learning!).

    Many thanks!
    Dominic

  • The topic ‘Custom Post Query where Meta value equals the current post's meta value’ is closed to new replies.