Wednesday, June 1, 2022
HomeWordPress Developmentcustomization - WP Search Kind Question: Add Writer's posts into search question

customization – WP Search Kind Question: Add Writer’s posts into search question


Because the title suggests, I am on the lookout for WordPress recommendation relative to leveraging the usual searchform.php performance to moreover question the writer area when performing searches. I’ve tried numerous completely different means to do that with none success, and so I am coming to fairly the headache. Beneath are the template recordsdata used:

searchform.php

<?php
/**
 * Template for displaying search varieties in Twenty Seventeen
 *
 * @bundle WordPress
 * @subpackage Twenty_Seventeen
 * @since 1.0
 * @model 1.0
 */

?>

<?php $unique_id = esc_attr( uniqid( 'search-form-' ) ); ?>

<div class="global-search">
  <kind function="search" methodology="get" class="search-form" motion="<?php echo esc_url( home_url( "https://wordpress.stackexchange.com/" ) ); ?>">
    <label for="<?php echo $unique_id; ?>">
      <span class="screen-reader-text sr-only"><?php echo _x( 'Seek for:', 'label', 'sdwp' ); ?></span>
    </label>
    <enter kind="search" id="<?php echo $unique_id; ?>" class="search-field" worth="<?php echo get_search_query(); ?>" identify="s" />
    <button kind="submit" class="search-btn"><i class="fas fa-search"></i><span class="screen-reader-text sr-only"><?php echo _x( 'Search', 'submit button', 'sdwp' ); ?></span></button>
  </kind>
</div>

capabilities.php – the little bit of code I’ve written up after a lot reiteration

operate add_author_to_query( $question ) {

  //don't run on admin aspect
  if( is_admin() ) return;

  if ( $query->is_main_query() && $query->is_search() ) {

    $submitted_data = get_search_query();

    // Get all customers who might match the submitted knowledge
    $customers = new WP_User_Query( array(
      'search' => $submitted_data,
      'search_columns' => array( 'user_login', 'user_nicename')
    ) );
    $authors = $users->get_results();

    foreach($authors as $writer) {
      $author_ids[] = $author->ID;
    }

    if( ! empty( $authors ) ) {
        $query->set('post_type', array( 'publish', 'article', 'web page', 'bio', 'writer', 'article_type'));
        $query->set( 'author__in', $author_ids );
        // $query->set( 'author__in', $authors );
    }
}
add_action( 'pre_get_posts', 'add_author_to_query' );

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments