wp_query "private" entries... plus only specific category (4 posts)

  1. adamdexter
    Posted 7 years ago #

    Ok, so I've been working on this site and I want the front page to post the most recent post. The majority of the posts in that I wanted to post are "private". I'm currently using get_posts to do this, for some reason, get_posts can override the fact that posts are private by default and see them. Here is where I run into a problem-

    1) get_posts cannot pass a category restriction (I have one in there, it does nothing). So i will just pull up the most recent post, regardless of the cat.
    2) wp_query can do more, but I can't get it to search the "private" posts, it doesn't see the category its in either because all the posts in that cat are private.

    What I need (to restate): Code that will search through all my posts, public and private, pull X number of posts from a Y only category and publish it.

    This is code I currently have:

    <?php $posts = get_posts( "category=interviews&numberposts=1" ); ?>
    <?php if( $posts ) : ?>
    <?php foreach( $posts as $post ) : setup_postdata( $post ); ?>
    <div class="post">
    <?php echo $post->post_excerpt; ?></div>
    <?php endforeach; ?>
    <?php endif; ?>

    Any help would be awesome.

  2. Kafkaesqui

    Posted 7 years ago #

    You'll have to go with a custom query for this. Modify your $posts statement to:

    $cat_id = 10; // category ID number
    $num_posts = 5; // # of posts to display
    $posts = $wpdb->get_results("SELECT * FROM $wpdb->posts INNER JOIN $wpdb->term_relationships ON $wpdb->posts.ID = $wpdb->term_relationships.object_id INNER JOIN $wpdb->term_taxonomy ON $wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id WHERE $wpdb->term_taxonomy.term_id = '$cat_id' AND post_type='post' ORDER BY post_date DESC LIMIT $num_posts");
  3. adamdexter
    Posted 7 years ago #

    I'm trying this and received an error:

    Fatal error: Call to a member function on a non-object in /homepages/23/d236268553/htdocs/wp-content/themes/k2/app/modules/php.php(7) : eval()'d code on line 5

    Is the K2 theme messing with it?

  4. dejvik
    Posted 7 years ago #

    Try to insert global $wpdb; before calling $wpdb->get_results.

Topic Closed

This topic has been closed to new replies.

About this Topic