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.

Ok here's the solution:

<?php 
$wp_user_search1 = new WP_User_Query( array(
'meta_key' => 'state' , 
'meta_value' => 'NM',
));
$listers1 = $wp_user_search1->get_results();

$lister_ids1 = array();
foreach($listers1 as $lister1) {
    $lister_ids1[] = $lister1->ID;
}

$ids1 = implode(',', $lister_ids1);

$wp_user_search2 = new WP_User_Query( array(
'meta_key' => 'state' , 
'meta_value' => 'TX',
));
$listers2 = $wp_user_search2->get_results();

$lister_ids2 = array();
foreach($listers2 as $lister2) {
    $lister_ids2[] = $lister2->ID;
}

$ids2 = implode(',', $lister_ids2);

$wp_user_search3 = new WP_User_Query( array(
'meta_key' => 'state' , 
'meta_value' => 'OK',
));
$listers3 = $wp_user_search3->get_results();

$lister_ids3 = array();
foreach($listers3 as $lister3) {
    $lister_ids3[] = $lister3->ID;
}

$ids3 = implode(',', $lister_ids3);

$wp_user_search4 = new WP_User_Query( array(
'meta_key' => 'state' , 
'meta_value' => 'TX',
));
$listers4 = $wp_user_search4->get_results();

$lister_ids4 = array();
foreach($listers4 as $lister4) {
    $lister_ids4[] = $lister4->ID;
}

$ids4 = implode(',', $lister_ids4);

$wp_user_search5 = new WP_User_Query( array(
'meta_key' => 'state' , 
'meta_value' => 'TX',
));
$listers5 = $wp_user_search5->get_results();

$lister_ids5 = array();
foreach($listers5 as $lister5) {
    $lister_ids5[] = $lister5->ID;
}

$ids5 = implode(',', $lister_ids5);

$wp_user_search6 = new WP_User_Query( array(
'meta_key' => 'state' , 
'meta_value' => 'TX',
));
$listers6 = $wp_user_search6->get_results();

$lister_ids6 = array();
foreach($listers6 as $lister6) {
    $lister_ids6[] = $lister6->ID;
}

$ids6 = implode(',', $lister_ids6);

$ids = $ids1.",". $ids2.",". $ids3.",". $ids4.",". $ids5.",". $ids6;
$args = array(
'post_type' => 'listing',
'author' => $ids,
'posts_per_page' => 10,
'paged' => $paged
);

$your_query = new WP_Query( $args );
if ( have_posts() ) : while ( $your_query->have_posts() ) : $your_query->the_post();?>
share|improve this question
    
I have it working for one meta value, but need it for multiple. –  eteich Nov 15 '11 at 23:11
    
I had to concatenate all the returned variables for each meta value –  eteich Nov 16 '11 at 0:15
add comment

4 Answers

up vote 0 down vote accepted

$args is a variable and as such should not be wrapped in quotes in the arguments array of query_posts(). Now that should be what's causing the whole thing to fail.

That being said, there are a few more flaws I see in your code:

  • I'd recommend using the WP_Query class instead of query_posts() (why is that?)
  • While it works, I'd not use $args as a variable name unless it's actually all the arguments to a wp function. Just to stick to consistent syntax.
  • Why are you calling global $query_string; if you're not using it in the query itsself?

You're good till the foreach loop has finished. The whole thing should look something like this:

$your_user_search = new WP_User_Query( array(
    'meta_key' => 'state' , 
    'meta_value' => 'NM'
    ));
$listers = $your_user_search->get_results();
$lister_ids = array();
foreach($listers as $lister) {
    $lister_ids[] = $lister->ID;
}
// query arguments
$authors = implode(',', $lister_ids);
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$args = array(
    'post_type' => 'listing',
    'author' => $authors,
    'posts_per_page' => 10,
    'paged' => $paged
);
// query for posts
$your_query = new WP_Query( $args );
// loop through found posts
while ( $your_query->have_posts() ) : $your_query->the_post();
    // inside of loop, echo markup and post content here
endwhile;
// pagination, with check for WP-PageNavi plugin
if ( function_exists('wp_pagenavi') ) {
    wp_pagenavi( array( 'query' => $your_query ) );
} elseif ( get_next_posts_link() || get_previous_posts_link() ) {
    // optional: echo markup around links
    next_posts_link( '&laquo; Older Entries', $your_query->max_num_pages );
    previous_posts_link( 'Newer Entries &raquo;' );
}
wp_reset_postdata(); // reset post data, important!
share|improve this answer
    
although this helps clean up my loop (can't get the pagination to work though) the wpdb statement is not drawing any of the author id's –  eteich Nov 15 '11 at 22:35
    
@eteich Seeing now that you edited your code. Why? The $wp_user_search = new WP_User_Query(...) bit was totally fine! –  Johannes Pille Nov 15 '11 at 22:42
    
@eteich Also see the arguments passed to either wp_pagenavi() or next_post_link(). The above should now pretty much be copy&paste ready... –  Johannes Pille Nov 15 '11 at 23:16
    
I got it all working now. Thanks @Johannes Pile. –  eteich Nov 16 '11 at 0:13
add comment

Line 6 of your code has a typo - "litsers" instead of "listers" (line 9).

share|improve this answer
    
Fixed it, but I still am getting no output. –  eteich Nov 15 '11 at 20:01
add comment

The main idea is right but just one thing wrong, your are selecting user_id and later you try to access id which is not the the id of the user, but the id of the meta entry.

try this:

global $wpdb;
$query = "SELECT DISTINCT user_id
    FROM $wpdb->usermeta
    WHERE meta_key = 'state'
    AND meta_value = 'NM'";
$author_ids = $wpdb->get_results($query);
$lister_ids = implode(',', $author_ids);
$listq = new WP_Query(array( 
    'post_type' => 'listing', 
    'author' => $lister_ids , 
    'posts_per_page' => '10' ,  
    'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1)
    )
);
if ( $listq->have_posts() ) : while ( $listq->have_posts() ) : $listq->the_post(); ?>
share|improve this answer
    
plugged that in an it didn't work for me. –  eteich Nov 15 '11 at 23:26
    
I would love to use this method, because I could do all the searching in one fell swoop, but I cannot get the outputted sql formatted to just the user id's seperated by commas. –  eteich Nov 17 '11 at 17:05
add comment

Here's the entire fix... It's a lot of server load, but it works for the implementation that was already in place.

Discovers users by state for a certain zone and displays all posts in a table.

<?php 
$wp_user_search1 = new WP_User_Query( array(
'meta_key' => 'state' , 
'meta_value' => 'TN',
));
$listers1 = $wp_user_search1->get_results();

$lister_ids1 = array();
foreach($listers1 as $lister1) {
    $lister_ids1[] = $lister1->ID;
}

$ids1 = implode(',', $lister_ids1);

$wp_user_search2 = new WP_User_Query( array(
'meta_key' => 'state' , 
'meta_value' => 'NC',
));
$listers2 = $wp_user_search2->get_results();

$lister_ids2 = array();
foreach($listers2 as $lister2) {
    $lister_ids2[] = $lister2->ID;
}

$ids2 = implode(',', $lister_ids2);

$wp_user_search3 = new WP_User_Query( array(
'meta_key' => 'state' , 
'meta_value' => 'SC',
));
$listers3 = $wp_user_search3->get_results();

$lister_ids3 = array();
foreach($listers3 as $lister3) {
    $lister_ids3[] = $lister3->ID;
}

$ids3 = implode(',', $lister_ids3);

$wp_user_search4 = new WP_User_Query( array(
'meta_key' => 'state' , 
'meta_value' => 'AL',
));
$listers4 = $wp_user_search4->get_results();

$lister_ids4 = array();
foreach($listers4 as $lister4) {
    $lister_ids4[] = $lister4->ID;
}

$ids4 = implode(',', $lister_ids4);

$wp_user_search5 = new WP_User_Query( array(
'meta_key' => 'state' , 
'meta_value' => 'GA',
));
$listers5 = $wp_user_search5->get_results();

$lister_ids5 = array();
foreach($listers5 as $lister5) {
    $lister_ids5[] = $lister5->ID;
}

$ids5 = implode(',', $lister_ids5);

$wp_user_search6 = new WP_User_Query( array(
'meta_key' => 'state' , 
'meta_value' => 'FL',
));
$listers6 = $wp_user_search6->get_results();

$lister_ids6 = array();
foreach($listers6 as $lister6) {
    $lister_ids6[] = $lister6->ID;
}

$ids6 = implode(',', $lister_ids6);

$wp_user_search7 = new WP_User_Query( array(
'meta_key' => 'state' , 
'meta_value' => 'FL',
));
$listers7 = $wp_user_search7->get_results();

$lister_ids7 = array();
foreach($listers7 as $lister7) {
    $lister_ids7[] = $lister7->ID;
}

$ids7 = implode(',', $lister_ids7);

$wp_user_search8 = new WP_User_Query( array(
'meta_key' => 'state' , 
'meta_value' => 'FL',
));
$listers8 = $wp_user_search8->get_results();

$lister_ids8 = array();
foreach($listers8 as $lister8) {
    $lister_ids8[] = $lister8->ID;
}

$ids8 = implode(',', $lister_ids8);

$wp_user_search9 = new WP_User_Query( array(
'meta_key' => 'state' , 
'meta_value' => 'FL',
));
$listers9 = $wp_user_search9->get_results();

$lister_ids9 = array();
foreach($listers9 as $lister9) {
    $lister_ids9[] = $lister9->ID;
}

$ids9 = implode(',', $lister_ids9);

$wp_user_search10 = new WP_User_Query( array(
'meta_key' => 'state' , 
'meta_value' => 'FL',
));
$listers10 = $wp_user_search10->get_results();

$lister_ids10 = array();
foreach($listers10 as $lister10) {
    $lister_ids10[] = $lister10->ID;
}

$ids10 = implode(',', $lister_ids10);

$wp_user_search11 = new WP_User_Query( array(
'meta_key' => 'state' , 
'meta_value' => 'FL',
));
$listers11 = $wp_user_search11->get_results();

$lister_ids11 = array();
foreach($listers11 as $lister11) {
    $lister_ids11[] = $lister11->ID;
}

$ids11 = implode(',', $lister_ids11);

$ids = $ids1.",". $ids2.",". $ids3.",". $ids4.",". $ids5.",". $ids6.",". $ids7.",". $ids8.",". $ids9.",". $ids10.",". $ids11;
$args = array(
'post_type' => 'listing',
'author' => $ids,
'posts_per_page' => 10,
'paged' => $paged
);

$your_query = new WP_Query( $args );
if ( have_posts() ) : while ( $your_query->have_posts() ) : $your_query->the_post();?>

<?php   
    $custom = get_post_custom($post->ID);
    $screenshot_url = $custom["screenshot_url"][0];
    $website_url = $custom["website_url"][0];
?>
<tr>
    <td class="td50 light"><div class="front_img"><a href="<?=$website_url?>"><?php postimage('thumbnail'); ?></a></div></td>
    <td class="td357 light"><a href="<?php the_permalink(); ?>"><strong><?php the_title(); ?></strong></a></td>
    <td class="td40 light"><?php the_field( 'package_size' ); ?></td>
    <td class="td40 light"><?php the_field( 'product_condition' ); ?></td>
    <td class="td40 light"><?php the_field( 'product_quantity' ); ?></td>
    <td class="td40 light">$<?php the_field( 'asking_price' ); ?><br />Per&nbsp;<?php the_field( 'package_quantity' ); ?></td>
    <td class="td40 light">

        <?php if (get_the_author_meta('state') == 'WA') { ?>1
        <?php } elseif (get_the_author_meta('state') == 'OR') { ?>1
        <?php } elseif (get_the_author_meta('state') == 'ID') { ?>1
        <?php } elseif (get_the_author_meta('state') == 'AZ') { ?>2
        <?php } elseif (get_the_author_meta('state') == 'CA') { ?>2
        <?php } elseif (get_the_author_meta('state') == 'NV') { ?>2
        <?php } elseif (get_the_author_meta('state') == 'UT') { ?>2
        <?php } elseif (get_the_author_meta('state') == 'MT') { ?>3
        <?php } elseif (get_the_author_meta('state') == 'WY') { ?>3
        <?php } elseif (get_the_author_meta('state') == 'ND') { ?>3
        <?php } elseif (get_the_author_meta('state') == 'SD') { ?>3
        <?php } elseif (get_the_author_meta('state') == 'CO') { ?>4
        <?php } elseif (get_the_author_meta('state') == 'NE') { ?>4
        <?php } elseif (get_the_author_meta('state') == 'KS') { ?>4
        <?php } elseif (get_the_author_meta('state') == 'IA') { ?>4
        <?php } elseif (get_the_author_meta('state') == 'MO') { ?>4
        <?php } elseif (get_the_author_meta('state') == 'NM') { ?>5
        <?php } elseif (get_the_author_meta('state') == 'TX') { ?>5
        <?php } elseif (get_the_author_meta('state') == 'OK') { ?>5
        <?php } elseif (get_the_author_meta('state') == 'AR') { ?>5
        <?php } elseif (get_the_author_meta('state') == 'MS') { ?>5
        <?php } elseif (get_the_author_meta('state') == 'LA') { ?>5
        <?php } elseif (get_the_author_meta('state') == 'MN') { ?>6
        <?php } elseif (get_the_author_meta('state') == 'WI') { ?>6
        <?php } elseif (get_the_author_meta('state') == 'IL') { ?>6
        <?php } elseif (get_the_author_meta('state') == 'IN') { ?>6
        <?php } elseif (get_the_author_meta('state') == 'MI') { ?>6
        <?php } elseif (get_the_author_meta('state') == 'OH') { ?>6
        <?php } elseif (get_the_author_meta('state') == 'KY') { ?>6
        <?php } elseif (get_the_author_meta('state') == 'WV') { ?>6
        <?php } elseif (get_the_author_meta('state') == 'VA') { ?>6
        <?php } elseif (get_the_author_meta('state') == 'TN') { ?>7
        <?php } elseif (get_the_author_meta('state') == 'NC') { ?>7
        <?php } elseif (get_the_author_meta('state') == 'SC') { ?>7
        <?php } elseif (get_the_author_meta('state') == 'AL') { ?>7
        <?php } elseif (get_the_author_meta('state') == 'GA') { ?>7
        <?php } elseif (get_the_author_meta('state') == 'FL') { ?>7
        <?php } elseif (get_the_author_meta('state') == 'PA') { ?>8
        <?php } elseif (get_the_author_meta('state') == 'NY') { ?>8
        <?php } elseif (get_the_author_meta('state') == 'ME') { ?>8
        <?php } elseif (get_the_author_meta('state') == 'VT') { ?>8
        <?php } elseif (get_the_author_meta('state') == 'NH') { ?>8
        <?php } elseif (get_the_author_meta('state') == 'MA') { ?>8
        <?php } elseif (get_the_author_meta('state') == 'RI') { ?>8
        <?php } elseif (get_the_author_meta('state') == 'CT') { ?>8
        <?php } elseif (get_the_author_meta('state') == 'NJ') { ?>8
        <?php } elseif (get_the_author_meta('state') == 'DE') { ?>8
        <?php } elseif (get_the_author_meta('state') == 'MD') { ?>8
        <?php } elseif (get_the_author_meta('state') == 'AK') { ?>9
        <?php } elseif (get_the_author_meta('state') == 'HI') { ?>9
        <?php } else { ?>&nbsp;<?php } ?>

    </td>
</tr>
<?php endwhile; endif; ?>
</table>
<div id="paginav">
<div class="alignright">
<?php if ( function_exists('wp_pagenavi') ) {
      wp_pagenavi( array( 'query' => $your_query ) );
      }
      elseif ( get_next_posts_link() || get_previous_posts_link() )
 previous_posts_link( 'Previous &raquo;' );

 next_posts_link( '&laquo; Next', $your_query->max_num_pages );
?>

<?php wp_reset_query(); ?>
share|improve this answer
add comment

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.