WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] query posts by post ID variable (2 posts)

  1. luke.faragher
    Member
    Posted 1 year ago #

    Hi really struggling with something I thought would be quite straightforward and hoping that some one can help...

    I essentially need to query posts by a list of post ID which are stored and then retrieved in a custom table in my database.

    Current Code...

    <?php
    $block = $wpdb->get_results( "SELECT sid FROM wp_entries WHERE uid = '$current_user->ID'" , ARRAY_N);
    
    	print_r ($block);
    
       $args = array('cat' => 8, 'showposts' => 3, 'post__not_in' => array($block) );
       $category_posts = new WP_Query($args);
    
       if($category_posts->have_posts()) :
          while($category_posts->have_posts()) :
             $category_posts->the_post();
    ?>

    Unfortunately all i'm getting back for this is the printed array ...

    Array ( [0] => Array ( [0] => 835 ) [1] => Array ( [0] => 832 ) [2] => Array ( [0] => 823 ) )

    But the posts are still displaying??

    Im fairly sure that i'm missing something fundamental but have no idea where to start.

    Thank you in advance for any help

  2. luke.faragher
    Member
    Posted 1 year ago #

    Solved it for anyone else who might find this useful...

    //This is the query that gets the blocked Post IDs
    
    		$things = array();
    		$results = $wpdb->get_results( "SELECT sid FROM wp_entries WHERE uid = '$current_user->ID'");
    		foreach( $results as $result )
    		$things[] = $result->sid;
    
    //This is the query that blocks the posts
    
       $args = array('cat' => 8, 'showposts' => 3, 'post__not_in' => $things );
       $category_posts = new WP_Query($args);
    
       if($category_posts->have_posts()) :
          while($category_posts->have_posts()) :
             $category_posts->the_post();

Topic Closed

This topic has been closed to new replies.

About this Topic