admin管理员组

文章数量:1130349

I have a page where published posts are listed and where it is possible to search for one or more posts by keyword from a simple search engine.

This is the part of the DOM that contains the search engine and the list of published posts.

<main>
        <h1>Articoli</h1>
        <form action="<?php echo get_page_url() ?>" method="get">
          <label for="search">Cerca</label>
          <input type="text" name="s" id="search" value="<?php the_search_query(); ?>" />
        </form>
        <?php if ($posts->have_posts()) : ?>
          <div id="wrp-articles">
            <?php while ($posts->have_posts()) : $posts->the_post() ?>
              <article>
                <div class="wrp-thumb">
                  <a href="<?php echo get_permalink() ?>">
                    <img src="<?php echo get_the_post_thumbnail_url() ?>" alt="">
                  </a>
                </div>
                <ul class="list-categories">
                  <?php foreach (get_the_category() as $cat) : ?>
                    <li>
                      <a href="<?php echo get_category_link($cat->term_id) ?>"><?php echo $cat->name ?></a>
                    </li>
                  <?php endforeach; ?>
                </ul>
                <span class="title"><?php echo get_the_title() ?></span>
                <p><?php echo get_the_excerpt() ?></p>
              </article>
            <?php endwhile; ?>
          </div>
        <?php endif; ?>
        <div id="pagination">
          <?php echo paginate_links(array(

            'base' => get_site_url() . '%_%',
            'format' => '/page/%#%',
            'total'  => $posts->max_num_pages,
            'current' => max(1, get_query_var('paged'))

          )) ?>
        </div>
      </main>

Here, on the other hand, I would like the value entered in the ‘s’ text field to be automatically searched by the WP_Query class in part of the title, in part of the description, in one or more categories and subcategories, and in one or more tags, but at the moment I have not yet found a way to do this. Currently the ‘s’ parameter only searches in the title and description.

<?php

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

$args = array(

  'post_type' => 'post',
  'posts_per_page' => 3,
  'order' => 'desc',
  'paged' => $paged

);

if (!empty($_GET['s'])) {

  $args['s'] = sanitize_text_field($_GET['s']);

}

$posts = new WP_Query($args);

?>

For example, if I search for the keyword ‘wordpress’, I would like the WP_Query class to return all the posts in which it is contained in the title, description, categories and tags. Is it possible to do this without using plugins and without modifying the functions.php file?

I have a page where published posts are listed and where it is possible to search for one or more posts by keyword from a simple search engine.

This is the part of the DOM that contains the search engine and the list of published posts.

<main>
        <h1>Articoli</h1>
        <form action="<?php echo get_page_url() ?>" method="get">
          <label for="search">Cerca</label>
          <input type="text" name="s" id="search" value="<?php the_search_query(); ?>" />
        </form>
        <?php if ($posts->have_posts()) : ?>
          <div id="wrp-articles">
            <?php while ($posts->have_posts()) : $posts->the_post() ?>
              <article>
                <div class="wrp-thumb">
                  <a href="<?php echo get_permalink() ?>">
                    <img src="<?php echo get_the_post_thumbnail_url() ?>" alt="">
                  </a>
                </div>
                <ul class="list-categories">
                  <?php foreach (get_the_category() as $cat) : ?>
                    <li>
                      <a href="<?php echo get_category_link($cat->term_id) ?>"><?php echo $cat->name ?></a>
                    </li>
                  <?php endforeach; ?>
                </ul>
                <span class="title"><?php echo get_the_title() ?></span>
                <p><?php echo get_the_excerpt() ?></p>
              </article>
            <?php endwhile; ?>
          </div>
        <?php endif; ?>
        <div id="pagination">
          <?php echo paginate_links(array(

            'base' => get_site_url() . '%_%',
            'format' => '/page/%#%',
            'total'  => $posts->max_num_pages,
            'current' => max(1, get_query_var('paged'))

          )) ?>
        </div>
      </main>

Here, on the other hand, I would like the value entered in the ‘s’ text field to be automatically searched by the WP_Query class in part of the title, in part of the description, in one or more categories and subcategories, and in one or more tags, but at the moment I have not yet found a way to do this. Currently the ‘s’ parameter only searches in the title and description.

<?php

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

$args = array(

  'post_type' => 'post',
  'posts_per_page' => 3,
  'order' => 'desc',
  'paged' => $paged

);

if (!empty($_GET['s'])) {

  $args['s'] = sanitize_text_field($_GET['s']);

}

$posts = new WP_Query($args);

?>

For example, if I search for the keyword ‘wordpress’, I would like the WP_Query class to return all the posts in which it is contained in the title, description, categories and tags. Is it possible to do this without using plugins and without modifying the functions.php file?

本文标签: