• Hi,
    I need to display a post that has a specific ID (obtained from the custom field of the current post) and (for extra safety reasons) the author ID is the same as the current post.

    I think I have to use a custom query, something like this:

    <?php
    $thisuser_id = get_the_author_ID();
    $main_related_post = get_post_meta($post->ID, main_post, true);
    
    global $wpdb;
    global $post;
    $querystr = "
    SELECT wposts.*
    FROM $wpdb->posts wposts
    		AND wposts.post_status = 'publish'
    		AND ID = '$main_related_post'
    		AND AUTHOR ID = '$thisuser_id'
    ORDER BY wpostmeta.meta_value ASC
    LIMIT 1
    ";
    $pageposts = $wpdb->get_results($querystr, OBJECT);
    if ($pageposts):?>
       <small>
       <?php _e("Main Related Post:"); ?>
       </small>
    		<ul>
                <li class="article">
    					<a class="artilink" href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>">
    						<?php the_title(); ?>
    					</a>
                </li>
    		</ul>
    <?php endif; ?>

    please forgive my bad fancy code..
    thanks for your attention

  • The topic ‘Custom Query by author and post ID’ is closed to new replies.