Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

So i want to retrive a list with the posts that are in the meta_key "_blocks" and in that there is a multidimensional array with "areas" called left, right ,top etc. And within them there are assigned post_id:s. Like this:

Array
(
[left] => Array
(
[0] => 450
[1] => 452
[2] => 460
[3] => 451
[4] => 454
[5] => 449
[6] => 455
)

[right] => Array
(
[0] => 460
[1] => 455
[2] => 454
[3] => 453
)

)

The query look like this now.. Im a little confused right now how to archive this..

Before this im running a foreach as you can see on $areas as $area. And $type_areas holds etch post_type:s.

$args = array(
    'post_type' => $type_areas[$area['area']],
    'posts_per_page' => '-1',
    'meta_key' => '_blocks',
    'meta_query' => array(
      array(
        'key' => 'left',
        'value' =>  '450', // just testing...
        'compare' => 'IN',
      )
    )
);

$query = new WP_Query( $args );

while ( $query->have_posts() ) : $query->the_post();

   $output .= '<li data-id="'. $query->post->ID .'" data-area="'. $area['area'] .'"><p>'. get_the_title() .'</p><span>'. $query->post->post_type .'</span></li>';

endwhile;

UPDATE

So i ended upp with a simple SQL-query:

$sql = 'SELECT post_id FROM kwido_postmeta WHERE meta_value REGEXP \'"'. $area['area'] .'";a:[[:digit:]]+:{[^}]*"'. $mainpost->ID .'"\'';
$results = $wpdb->get_results( $sql, ARRAY_A );

That looks after the current post_id and area with a regex, then if found get the post_id..

$post_ids = array();

foreach ( $results as $result ) {
    $post_ids[] = $result['post_id'];
}

With the post_id:s i make the query with post__in parameter which accept arrays.

$args = array(
    'post_type' => $type_areas[$area['area']],
    'posts_per_page' => '-1',
    'post__not_in'   => $post_ids
);

$query = new WP_Query( $args );
share|improve this question

1 Answer 1

up vote 0 down vote accepted

Solved

So i ended upp with a simple SQL-query:

$sql = 'SELECT post_id FROM kwido_postmeta WHERE meta_value REGEXP \'"'. $area['area'] .'";a:[[:digit:]]+:{[^}]*"'. $mainpost->ID .'"\'';
$results = $wpdb->get_results( $sql, ARRAY_A );

That looks after the current post_id and area with a regex, then if found get the post_id..

$post_ids = array();

foreach ( $results as $result ) {
    $post_ids[] = $result['post_id'];
}

With the post_id:s i make the query with post__in parameter which accept arrays.

$args = array(
    'post_type' => $type_areas[$area['area']],
    'posts_per_page' => '-1',
    'post__not_in'   => $post_ids
);

$query = new WP_Query( $args );
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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