2 added 101 characters in body
source | link

After reading a bunch of tips for running a WP_Query filtering by serialized arrays, here's how I finally did it: by creating an array of comma separated values using implode in conjunction with a $wpdb custom SQL query utilizing FIND_IN_SET to search the comma separated list for the requested value.

(this is similar to Tomas's answer, but its a bit less performance intensive for the SQL query)

1. In functions.php:

In your functions.php file (or wherever you're setting up the meta box) in the yourname_save_post() function use

update_post_meta($post->ID, 'checkboxArray', implode(",", $checkboxArray)); //adding the implode

to create the array containing comma separated values.

You'll also want to change your output variable in the yourname_post_meta() admin meta box construction function to

$checkboxArray = explode(",", get_post_custom($post->ID)["checkboxArray"][0]); //adding the explode

2. In the template PHP file:

Test: if you run a get_post_meta( $id ); you should see checkboxArray as an array containing your comma separated values instead of a serialized array.

Now, we build our custom SQL query using $wpdb.

global $wpdb;

$search = $post->ID;

$query = "SELECT * FROM wp_posts
          WHERE FIND_IN_SET( $search, (
              SELECT wp_postmeta.meta_value FROM wp_postmeta
              WHERE wp_postmeta.meta_key = 'blogLocations'
              AND wp_postmeta.post_id = wp_posts.ID )
          )
          AND ( wp_posts.post_type = 'post' )
          AND ( wp_posts.post_status = 'publish' );";

$posts = $wpdb->get_results($query);

foreach ($posts as $post) {
    //your post content here
}

Notice the FIND_IN_SET, that's where the magic happens.

Now... since I'm using SELECT * this returns all the post data and within the foreach you can echo out what you want from that (do a print_r($posts); if you don't know what's included. It doesn't set up "the loop" for you (I prefer it this way), but it can easily be modified to set up the loop if you prefer (take a look at setup_postdata($post); in the codex, you'll probably need to change SELECT * to select only post ID's and $wpdb->get_results to the correct $wpdb type -- see the codex for $wpdb also for information on that subject).

Whelp, it took a bit of effort, but since wp_query doesn't support doing 'compare' => 'IN' serialized or comma separated values this shim is your best option!

Hope this helps someone.

After reading a bunch of tips for running a WP_Query filtering by serialized arrays, here's how I finally did it: by creating an array of comma separated values using implode in conjunction with a $wpdb custom SQL query utilizing FIND_IN_SET to search the comma separated list for the requested value.

1. In functions.php:

In your functions.php file (or wherever you're setting up the meta box) in the yourname_save_post() function use

update_post_meta($post->ID, 'checkboxArray', implode(",", $checkboxArray)); //adding the implode

to create the array containing comma separated values.

You'll also want to change your output variable in the yourname_post_meta() admin meta box construction function to

$checkboxArray = explode(",", get_post_custom($post->ID)["checkboxArray"][0]); //adding the explode

2. In the template PHP file:

Test: if you run a get_post_meta( $id ); you should see checkboxArray as an array containing your comma separated values instead of a serialized array.

Now, we build our custom SQL query using $wpdb.

global $wpdb;

$search = $post->ID;

$query = "SELECT * FROM wp_posts
          WHERE FIND_IN_SET( $search, (
              SELECT wp_postmeta.meta_value FROM wp_postmeta
              WHERE wp_postmeta.meta_key = 'blogLocations'
              AND wp_postmeta.post_id = wp_posts.ID )
          )
          AND ( wp_posts.post_type = 'post' )
          AND ( wp_posts.post_status = 'publish' );";

$posts = $wpdb->get_results($query);

foreach ($posts as $post) {
    //your post content here
}

Notice the FIND_IN_SET, that's where the magic happens.

Now... since I'm using SELECT * this returns all the post data and within the foreach you can echo out what you want from that (do a print_r($posts); if you don't know what's included. It doesn't set up "the loop" for you (I prefer it this way), but it can easily be modified to set up the loop if you prefer (take a look at setup_postdata($post); in the codex, you'll probably need to change SELECT * to select only post ID's and $wpdb->get_results to the correct $wpdb type -- see the codex for $wpdb also for information on that subject).

Whelp, it took a bit of effort, but since wp_query doesn't support doing 'compare' => 'IN' serialized or comma separated values this shim is your best option!

Hope this helps someone.

After reading a bunch of tips for running a WP_Query filtering by serialized arrays, here's how I finally did it: by creating an array of comma separated values using implode in conjunction with a $wpdb custom SQL query utilizing FIND_IN_SET to search the comma separated list for the requested value.

(this is similar to Tomas's answer, but its a bit less performance intensive for the SQL query)

1. In functions.php:

In your functions.php file (or wherever you're setting up the meta box) in the yourname_save_post() function use

update_post_meta($post->ID, 'checkboxArray', implode(",", $checkboxArray)); //adding the implode

to create the array containing comma separated values.

You'll also want to change your output variable in the yourname_post_meta() admin meta box construction function to

$checkboxArray = explode(",", get_post_custom($post->ID)["checkboxArray"][0]); //adding the explode

2. In the template PHP file:

Test: if you run a get_post_meta( $id ); you should see checkboxArray as an array containing your comma separated values instead of a serialized array.

Now, we build our custom SQL query using $wpdb.

global $wpdb;

$search = $post->ID;

$query = "SELECT * FROM wp_posts
          WHERE FIND_IN_SET( $search, (
              SELECT wp_postmeta.meta_value FROM wp_postmeta
              WHERE wp_postmeta.meta_key = 'blogLocations'
              AND wp_postmeta.post_id = wp_posts.ID )
          )
          AND ( wp_posts.post_type = 'post' )
          AND ( wp_posts.post_status = 'publish' );";

$posts = $wpdb->get_results($query);

foreach ($posts as $post) {
    //your post content here
}

Notice the FIND_IN_SET, that's where the magic happens.

Now... since I'm using SELECT * this returns all the post data and within the foreach you can echo out what you want from that (do a print_r($posts); if you don't know what's included. It doesn't set up "the loop" for you (I prefer it this way), but it can easily be modified to set up the loop if you prefer (take a look at setup_postdata($post); in the codex, you'll probably need to change SELECT * to select only post ID's and $wpdb->get_results to the correct $wpdb type -- see the codex for $wpdb also for information on that subject).

Whelp, it took a bit of effort, but since wp_query doesn't support doing 'compare' => 'IN' serialized or comma separated values this shim is your best option!

Hope this helps someone.

1
source | link

After reading a bunch of tips for running a WP_Query filtering by serialized arrays, here's how I finally did it: by creating an array of comma separated values using implode in conjunction with a $wpdb custom SQL query utilizing FIND_IN_SET to search the comma separated list for the requested value.

1. In functions.php:

In your functions.php file (or wherever you're setting up the meta box) in the yourname_save_post() function use

update_post_meta($post->ID, 'checkboxArray', implode(",", $checkboxArray)); //adding the implode

to create the array containing comma separated values.

You'll also want to change your output variable in the yourname_post_meta() admin meta box construction function to

$checkboxArray = explode(",", get_post_custom($post->ID)["checkboxArray"][0]); //adding the explode

2. In the template PHP file:

Test: if you run a get_post_meta( $id ); you should see checkboxArray as an array containing your comma separated values instead of a serialized array.

Now, we build our custom SQL query using $wpdb.

global $wpdb;

$search = $post->ID;

$query = "SELECT * FROM wp_posts
          WHERE FIND_IN_SET( $search, (
              SELECT wp_postmeta.meta_value FROM wp_postmeta
              WHERE wp_postmeta.meta_key = 'blogLocations'
              AND wp_postmeta.post_id = wp_posts.ID )
          )
          AND ( wp_posts.post_type = 'post' )
          AND ( wp_posts.post_status = 'publish' );";

$posts = $wpdb->get_results($query);

foreach ($posts as $post) {
    //your post content here
}

Notice the FIND_IN_SET, that's where the magic happens.

Now... since I'm using SELECT * this returns all the post data and within the foreach you can echo out what you want from that (do a print_r($posts); if you don't know what's included. It doesn't set up "the loop" for you (I prefer it this way), but it can easily be modified to set up the loop if you prefer (take a look at setup_postdata($post); in the codex, you'll probably need to change SELECT * to select only post ID's and $wpdb->get_results to the correct $wpdb type -- see the codex for $wpdb also for information on that subject).

Whelp, it took a bit of effort, but since wp_query doesn't support doing 'compare' => 'IN' serialized or comma separated values this shim is your best option!

Hope this helps someone.