最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

customization - How to create custom search form function including post "tags"

programmeradmin1浏览0评论

This is my HTML:

<input type="text" class="keyword" name="keyword" id="keyword" placeholder="Search video...">
<button id="search-keyword">Search</button>

When I click the "Search" button start a script like this:

if($('.current').hasClass('search-select')){
    action = 'my_create_posts_search'; //php function to do 
    ab = document.getElementsByClassName('keyword')[0].value; //get the value of input
}

Then an Ajax function starts:

 $.ajax({
    type: "POST",
    url: ajax_params.ajax_url, //wordpress ajax
    data: {
     'action': action, //php function to do  
     'ab': ab, //value of input
},

Then the action start and the code excecuted is this:

function my_create_posts_search($name) {
 //Pagination for posts
 $response = '';
 $paged = (int) (!is_null($_POST['clicked_times'])) ? $_POST['clicked_times'] : 0;

 $postsPerPage = 18;
 $postOffset = $paged * $postsPerPage;

 $args = array(
    'posts_per_page'  => $postsPerPage, //pagination
    'offset'          => $postOffset, //pagination
    'category'        => 42, //post category
    's'               => $name, //value of input
    'orderby'         => 'date',
    'order'           => 'DESC',
 );
 ... other things...
}

Everything works perfectly expcet when I would like to search by Post Tags.

How can I implement this feature?

I hope my code will help you. Thanks!

This is my HTML:

<input type="text" class="keyword" name="keyword" id="keyword" placeholder="Search video...">
<button id="search-keyword">Search</button>

When I click the "Search" button start a script like this:

if($('.current').hasClass('search-select')){
    action = 'my_create_posts_search'; //php function to do 
    ab = document.getElementsByClassName('keyword')[0].value; //get the value of input
}

Then an Ajax function starts:

 $.ajax({
    type: "POST",
    url: ajax_params.ajax_url, //wordpress ajax
    data: {
     'action': action, //php function to do  
     'ab': ab, //value of input
},

Then the action start and the code excecuted is this:

function my_create_posts_search($name) {
 //Pagination for posts
 $response = '';
 $paged = (int) (!is_null($_POST['clicked_times'])) ? $_POST['clicked_times'] : 0;

 $postsPerPage = 18;
 $postOffset = $paged * $postsPerPage;

 $args = array(
    'posts_per_page'  => $postsPerPage, //pagination
    'offset'          => $postOffset, //pagination
    'category'        => 42, //post category
    's'               => $name, //value of input
    'orderby'         => 'date',
    'order'           => 'DESC',
 );
 ... other things...
}

Everything works perfectly expcet when I would like to search by Post Tags.

How can I implement this feature?

I hope my code will help you. Thanks!

Share Improve this question asked Nov 12, 2019 at 12:07 ElleElle 1132 silver badges6 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

If you are using WP_Query you can search for tags using this code:

$query = new WP_Query( array( 'tag' => 'cooking' ) );

For more tag options see this page: https://developer.wordpress/reference/classes/wp_query/#tag-parameters

发布评论

评论列表(0)

  1. 暂无评论