尝试在中显示具有特定用户元的作者的帖子

时间:2011-11-15 作者:eteich

好的,下面是解决方案:

<?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();?>

4 个回复
最合适的回答,由SO网友:Johannes Pille 整理而成

$args 是一个变量,因此不应在query_posts(). 这应该是导致整个事情失败的原因。

尽管如此,我在您的代码中看到了更多的缺陷:

我建议使用WP_Query 类而不是query_posts() (why is that?)$args 作为变量名,除非它实际上是wp函数的所有参数。只是为了坚持一致的语法global $query_string; 如果您没有在查询中使用它自己foreach 循环已完成。整个事情应该是这样的:

$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!

SO网友:GavinR

代码的第6行有一个输入错误——“litsers”而不是“listers”(第9行)。

SO网友:Bainternet

主要想法是对的,但有一点是错的,你正在选择user_id 然后你尝试访问id 它不是用户的id,而是元条目的id。

尝试以下操作:

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(); ?>

SO网友:eteich

这是整个修复过程。。。这需要大量的服务器负载,但它适用于已经到位的实现。

按特定区域的状态查找用户,并在表中显示所有帖子。

<?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(); ?>

结束

相关推荐

$wpdb在插件内的页面上似乎不起作用

我有一个插件文件夹foobar。在这个插件中,我有一个名为foobar的页面。php。在这页里我有global $wpdb; $orderby = (!empty($_REQUEST[\'orderby\'])) ? $_REQUEST[\'orderby\'] : \'name\'; //If no sort, default to title $sql = \"SELECT * FROM wp_nc_location ORDER BY \" .