How to customize user search

时间:2015-04-01 作者:DamianFox


我正在开发一个自定义用户搜索,我正在使用找到的这个插件here:

<?php

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

    extract(shortcode_atts(array(
        "role" => \'technician\',
        "number" => \'10\'
    ), $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
    $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 . \'*\',
          ));
    } else {
        // Generate the query 
        $my_users = new WP_User_Query( 
          array( 
            \'role\' => \'technician\'
          ));
    }

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

<style>

.advance-search .real-btn {
    margin: 0;
}

</style>

<section class="advance-search ">
    <form method="get" id="sul-searchform" class="advance-search-form clearfix" action="<?php the_permalink() ?>">
        <div class="option-bar small">
            <input type="text" class="field" name="as" id="sul-s" placeholder="Cerca tecnico" />
        </div>
        <div class="option-bar">
            <input type="submit" name="submit" class=" real-btn btn"  id="sul-searchsubmit" value="Cerca" />
        </div>
    </form>
  <?php 
  if($search) { ?>
    <h2>Ricerca: <em><?php echo $search; ?></em></h2>
    <!--<a href="<?php the_permalink(); ?>">Back To Author Listing</a>-->
  <?php } ?>

</section><!-- .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 if(!empty($author->pie_profile_pic_5)): ?>
                <img width="130" height="130" src="<?php echo $author->pie_profile_pic_5; ?>" 
                                                     class="attachment-agent-image wp-post-image" alt="<?php echo $author->first_name; ?> <?php echo $author->last_name; ?>">
                <h2 style="display: inline; margin-left: 20px;">
                    <a href="<?php echo get_author_posts_url($author->ID); ?>"><?php echo $author->first_name; ?> <?php echo $author->last_name; ?></a>
                    - <?php echo count_user_posts( $author->ID ); ?> immobili
                </h2>
                <?php else: ?>
                    <h2 style="display: inline;">
                        <a href="<?php echo get_author_posts_url($author->ID); ?>"><?php echo $author->first_name; ?> <?php echo $author->last_name; ?></a>
                        - <?php echo count_user_posts($author->ID, "property"); ?> immobili
                    </h2>
                <?php endif; ?>

              <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); ?> ">Maggiori informazioni.. <?php //echo $author_info->display_name; ?></a></p>-->
            </li>
            <?php
          }
        ?>
    </ul> <!-- .author-list -->
<?php } else { ?>
  <!--<h2>Nessun tecnico trovato</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">←</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">→</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\', \'sul_user_listing\');
?>
但我想按城市、州和国家扩展搜索范围
事实上wp_usermeta 表具有以下值:meta\\u键:pie_address_3;meta\\u值:a:6:{s:7:"address";s:18:"New York Street, 4";s:8:"address2";s:0:"";s:4:"city";s:5:"Venice";s:5:"state";s:7:"Vicenza";s:3:"zip";s:5:"36015";s:7:"country";s:5:"Italy";}

因此,我的问题是:如何使用这些元值来扩展搜索<谢谢!

UPDATE

我已更新WP_User_Query 像这样:

$args = array(
            \'meta_key\' => \'pie_address_3\'
        );
        $query = new WP_User_Query($args);

        $authors = $query->get_results();

        foreach ($authors as $author){
            $c = get_user_meta($author->id,\'pie_address_3\',true);
            if(isset($c)){
                if(empty(get_user_meta($author->id,\'address\',true))){
                   add_user_meta($author->ID,\'address\',$c[\'address\']); 
                }
                if(empty(get_user_meta($author->id,\'address2\',true))){
                   add_user_meta($author->ID,\'address2\',$c[\'address2\']); 
                }
                if(empty(get_user_meta($author->id,\'city\',true))){
                   add_user_meta($author->ID,\'city\',$c[\'city\']); 
                }
                if(empty(get_user_meta($author->id,\'state\',true))){
                   add_user_meta($author->ID,\'state\',$c[\'state\']); 
                }
                if(empty(get_user_meta($author->id,\'zip\',true))){
                   add_user_meta($author->ID,\'zip\',$c[\'zip\']); 
                }
                if(empty(get_user_meta($author->id,\'country\',true))){
                   add_user_meta($author->ID,\'country\',$c[\'country\']); 
                };
                //delete_user_meta($author->ID,\'pie_address_3\',$c);
            }
        }

        $my_users = new WP_User_Query(
          array( 
            \'role\' => $role,
            \'search\' => \'*\' . $search . \'*\',
            \'search_columns\' => array(
                \'user_login\',
                \'user_nicename\',
                \'user_email\',
                \'user_url\',
                \'display_name\'
            ),
            \'meta_query\' => array(
                \'relation\' => \'OR\',
                array(
                  \'key\'     => \'address\',
                  \'value\'   => $search,
                  \'compare\' => \'LIKE\'
                ),
                array(
                  \'key\'     => \'address2\',
                  \'value\'   => $search,
                  \'compare\' => \'LIKE\'
                ),
                array(
                  \'key\'     => \'city\',
                  \'value\'   => $search,
                  \'compare\' => \'LIKE\'
                ),
                array(
                  \'key\'     => \'state\',
                  \'value\'   => $search,
                  \'compare\' => \'LIKE\'
                ),
                array(
                  \'key\'     => \'zip\',
                  \'value\'   => $search,
                  \'compare\' => \'LIKE\'
                ),
                array(
                  \'key\'     => \'country\',
                  \'value\'   => $search,
                  \'compare\' => \'LIKE\'
                )
              )
          )
        );
但还是不行。我哪里做错了?

1 个回复
最合适的回答,由SO网友:DamianFox 整理而成

我找到了一个解决方案:

if(!empty($search)){
   $query = $wpdb->prepare( 
                 "SELECT DISTINCT user_id
                  FROM wp_usermeta
                  WHERE meta_key = \'pie_address_3\'
                  AND meta_value LIKE %s
                  ", \'%\' . $search . \'%\' );

                $ids = $wpdb->get_results($query); ?> 
                <ul class="author-list"> 
                    <?php
                    foreach ($ids as $id) { ?>
                        <li>
                            <img width="90" height="90" src="<?php echo get_user_meta($id->user_id, "pie_profile_pic_5", true); ?>" 
                                                                 class="attachment-agent-image wp-post-image">
                            <h2 style="display: inline; margin-left: 20px;">
                                <a href="<?php echo get_author_posts_url($id->user_id); ?>"><?php echo get_the_author_meta(\'first_name\', $id->user_id); ?> 
                                    <?php echo get_the_author_meta(\'last_name\', $id->user_id); ?>
                                </a>
                                - <?php echo count_user_posts($id->user_id); ?> immobili
                            </h2>
                        </li>
                    <?php } ?>
                </ul>
 <?php } ?>

结束

相关推荐

在加载plugins_后,get_plugins()不工作

知道为什么下面的代码function my_plugin_load() { get_plugins(); } add_action( \'plugins_loaded\', \'my_plugin_load\' ); 抛出此错误?Fatal error: 不应调用未定义的函数get\\u plugins()get_plugins() 定义在plugins_loaded 胡克开火了?如果不是,那么什么才是合适的钩子呢?(这个钩子应该启动插件的引导/加载过程)