使用搜索功能列出WordPress用户

时间:2013-03-14 作者:Osu

我很难完成从CosmosPlates优秀教程改编的这段代码。除了搜索功能外,一切都正常(列表和分页等),搜索功能不返回任何内容,即当我尝试按关键字筛选时,不会列出任何用户。

有人知道问题出在哪里吗?

谢谢你的指点。

Osu

<?php
/*
Plugin Name: WP Author Listing
Plugin URI: #
Description: Simple shortcode to list our WordPress users.
Author: OSU
Version: 1.0
Author URI: #
Licence: MIT

Many thanks for original plugin by: Cristian Antohe [http://cozmoslabs.com/]

*/

function osu_user_listing($atts, $content = null) {
    global $post;

    extract(shortcode_atts(array(
        "role" => \'\',
        "number" => \'5\'
    ), $atts));

    $role = sanitize_text_field($role);
    $number = sanitize_text_field($number);

    // We\'re outputting a lot of HTML, and the easiest way 
    // to do it is with output buffering from PHP.
    ob_start();

    // Get the Search Term
    // (condition) ? (true return value) : (false return value)
    $search = ( isset($_GET["as"]) ) ? sanitize_text_field($_GET["as"]) : false ;

    // Get Query Var for pagination. This already exists in WordPress
    $page = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;

    // Calculate the offset (i.e. how many users we should skip)
    $offset = ($page - 1) * $number;

    if ($search){
        // Generate the query based on search field
        $my_users = new WP_User_Query( 
            array( 
                \'role\' => $role, 
                \'search\' => \'*\' . $search . \'*\' // Assuming the problem is around here?
            ));
    } else {
        // Generate the query 
        $my_users = new WP_User_Query( 
            array( 
                \'role\' => \'\', // Default is all
                \'offset\' => $offset ,
                \'number\' => $number
            ));
    }


    // Get the total number of authors. Based on this, offset and number 
    // per page, we\'ll generate our pagination. 
    $total_authors = $my_users->total_users;

    // Calculate the total number of pages for the pagination
    $total_pages = intval($total_authors / $number) + 1;

    // The authors object. 
    $authors = $my_users->get_results();
?>

    <div class="author-search">
    <h2>Search authors by name</h2>
        <form method="get" id="sul-searchform" action="<?php the_permalink() ?>">
            <label for="as" class="assistive-text">Search</label>
            <input type="text" class="field" name="as" id="sul-s" placeholder="Search Authors" />
            <input type="submit" class="submit" name="submit" id="sul-searchsubmit" value="Search Authors" />
        </form>
    <?php 
    if($search){ ?>
        <h2 >Search Results for: <em><?php echo $search; ?></em></h2>
        <a href="<?php the_permalink(); ?>">Back To Author Listing</a>
    <?php } ?>

    </div><!-- .author-search -->

<?php if (!empty($authors))  { ?>
    <ul class="author-list">
<?php
    // loop through each author
    foreach($authors as $author){
        $author_info = get_userdata($author->ID);
        ?>
        <li>
            <?php echo get_avatar( $author->ID, 90 ); ?> 
            <h2><a href="<?php echo get_author_posts_url($author->ID); ?>"><?php echo $author_info->display_name; ?></a> - <?php echo count_user_posts( $author->ID ); ?> posts</h2>
            <p><?php echo $author_info->description; ?></p>
            <?php $latest_post = new WP_Query( "author=$author->ID&post_count=1" ); 
            if (!empty($latest_post->post)){ ?>
            <p><strong>Latest Article:</strong>
            <a href="<?php echo get_permalink($latest_post->post->ID) ?>">
                <?php echo get_the_title($latest_post->post->ID) ;?>
            </a></p>
            <?php } //endif ?>
            <p><a href="<?php echo get_author_posts_url($author->ID); ?> ">Read <?php echo $author_info->display_name; ?> posts</a></p>
        </li>
        <?php
    }
?>
    </ul> <!-- .author-list -->
<?php } else { ?>
    <h2>No authors found</h2>
<? } //endif ?>

    <nav id="nav-single" style="clear:both; float:none; margin-top:20px;">
        <h3 class="assistive-text">Post navigation</h3>
        <?php if ($page != 1) { ?>
            <span class="nav-previous"><a rel="prev" href="<?php the_permalink() ?>page/<?php echo $page - 1; ?>/"><span class="meta-nav">&lt;</span> Previous</a></span>
        <?php } ?>

        <?php if ($page < $total_pages ) { ?>
            <span class="nav-next"><a rel="next" href="<?php the_permalink() ?>page/<?php echo $page + 1; ?>/">Next <span class="meta-nav">&gt;</span></a></span>
        <?php } ?>
    </nav>


    <?php 
    // Output the content.
    $output = ob_get_contents();
    ob_end_clean();


    // Return only if we\'re inside a page. This won\'t list anything on a post or archive page. 
    if (is_page()) return  $output;

}

// Add the shortcode to WordPress. 
add_shortcode(\'userlisting\', \'osu_user_listing\');
?>

2 个回复
SO网友:Abdul Haseeb

这对我有用。

$user_query = new WP_User_Query( array(  \'search\' => \'*example.net*\', \'search_columns\' => array(\'user_url\') ));
$authors = $user_query->get_results();
搜索字符串中要使用的通配符是“*”,而不是“%”。此外,还必须将“search\\u columns”参数与以下可能的值一起包括在内

search_columns = array( \'user_nicename\', \'user_login\', \'user_email\', \'user_url\' )
重要的是,这些是用户表中的实际字段名。我使用\'search_columns\' => array(\'url\') 但它失败了。

SO网友:david.binda

我将发表我的评论。在我看来,从抄本上看,你可能也失踪了search_columns 参数。所以不仅仅是更换

\'search\' => \'*\' . $search . \'*\'
根据

\'search\' => \'%\' . $search . \'%\'
但也可能添加

search_columns = array( \'nicename\', \'login\', \'email\' ) 
参见法典:https://codex.wordpress.org/Class_Reference/WP_User_Query#Search_Parameters

结束

相关推荐

Whitelisting Commenters

我有一个经常收到垃圾邮件的博客,Akismet对此很在行。还不足以让我关掉节制。我想知道有没有一种方法可以将读者名单“白名单”,这样他们的评论就可以跳过审核而直接发布?