So I am attempting to filter out certain posts in the get_posts call. Essentially, I want to be able to specify a certain block of posts, and only get the posts that correspond with the block. Right now I have a table that contains both block IDs and post IDs, so I have been looking into using the posts_join and the posts_where hooks. However, I need to be able to dynamically modify the block retrieved depending on what the client currently has chosen. To this end, I am tracking what the client has selected using a cookie. I feel that I should be able to pass the block ID as a another value in the array given to get_posts, but I have no idea how to retrieve that value. So my question is: how should I go about doing this?

It seems that the simplest, most elegant solution would be to just add something like:

$postargs = array(
    'blockID' => $id
$posts = get_posts($postargs);

I just don't know how in my hooked function:

function set_block_filter($id){
    /* This should probably use the posts_join hook */
    /* $join = 'LEFT JOIN wdpress.$table_name ON wdpress.$table_name.post_id = wdpress.wp_posts.ID'*/
    /* something like that ^ http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_join*/
    /* $where = ' AND block_id=$id '*/

can retrieve the blockID from the $postargs array.

EDIT: So the solution was actually very simple, and I think most of my confusion was born from my misunderstanding about how cookies work. Since they are apparently accessible server-side, my answer was this:

function set_block_filter($query){
        $query->set('meta_key', 'block');
        $query->set('meta_value', $_COOKIE['block']);

  • Do you absolutely need these block ID relationships in a separate table? If not, set them as post meta, and you can query them with a meta query. This is the kind of thing that post meta is perfect for.
    – MathSmath
    Jun 1, 2012 at 21:26
  • I don't know. Perhaps it could work very well to use post meta... Maybe if I got rid of the table linking posts to blocks and just used the post meta table...? Hmm... I will look into this. I kind of did a cursory look at post meta and it didn't seem that it would work, but with closer consideration, perhaps it would. Jun 1, 2012 at 21:42

2 Answers 2


Why not use the include (or conversely, exclude) argument for get_posts()? It takes an array of post IDs.


Depending on the flow of interaction I would store the information in either user_meta or post_meta. If the user chooses a block and it doesn't change very ofter I would save it in the user_meta table. This is the same way WordPress tracks your current dashboard preferences in the admin panel.

Or you could use both. Save the different blocks in post_meta and the users current choice in user_meta.

I would use Ajax just because with WordPress it's so easy and you don't have to reload the page. This of course depends on your UI flow.

<select name="post-block" id="<?php echo get_current_user_id(); ?>">
<option value="_block-value1">Block Value 1</option>
<option value="_block-value2">Block Value 2</option>

    var userID = $("select [name='post-block']").attr("id");

    $("select [name="post-block"]").change(function() {
          var option ="";
        $("select option:selected").each(function () {
         option += $(this).val();
          $.ajax({ type: "POST", 
                    url: ajaxurl, 
                   data: {"action": "select-block", user_id: userID, user_option: option },
           success: function(response) {
           alert('The database said: ' + response);


    add_action( 'wp_ajax_nopriv_select-block', 'update_block_options' );
    function update_block_options() {
         $user_id = intval( $_POST['user_id'] );
         $block_option = $_POST['user_option'];

        $option_array = get_post_meta( $post->ID, $block_option, true);
          update_user_meta( $user_id, 'user_block_options', (array) $option_array );

          if (get_user_meta( $user_id,  'user_block_options' )
          $response = get_user_meta( $user_id,  'user_block_options';
          else $response = "Ooops something went wrong....";

           echo $response;

To get the options before you run get_posts:

 $exclude_array = get_user_meta( get_current_user_id(), 'user_block_options' );
 $posts = get_posts( array('exclude' => (array)($exclude_array() );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.