• Hi,
    I have been created custom search form in my WordPress theme.

    
    <form role="search" class="form" method="get" action="<?php echo home_url('/');?>">
      <input type="search" id="keyword" class="inp-search" onclick="showNewTag()" oninput="searchTags()" onkeyup="fetch()" placeholder="search by tag" value="<?php echo get_search_query() ?>" name="s" title="Search" />
    

    And I want sort search by title at first and then Content or just search by title,
    can you Pls tell me how can i write code in function.php?

    
    add_action( 'wp_footer', 'ajax_fetch' );
    function ajax_fetch() {
    ?>
    <script type="text/javascript">
      function fetch(){
        jQuery.ajax({
        url: '<?php echo admin_url('admin-ajax.php'); ?>',
        type: 'post',
        data: { action: 'data_fetch', keyword: jQuery('#keyword').val() },
        success: function(data) {
            jQuery('#grid').html( data );
        }
    });
    }
    </script>
    <?php
    }
    add_action('wp_ajax_data_fetch' , 'data_fetch');
    add_action('wp_ajax_nopriv_data_fetch','data_fetch');
    function data_fetch(){
    echo '<div class="split blog mob">';
    $the_query_post = new WP_Query( array( 'posts_per_page' => -1, 's' => esc_attr( $_POST['keyword'] ), 'post_type' => 'post', 'post_status' => 'publish' ,  'orderby' => 'menu_order title'  ,  'order' => 'DESC' ,
    ) );
    if( $the_query_post->have_posts() ) :
    echo '<div class="titl-sol"><h3 class="title-box">blog Parham</h3></div><ul>';
    while( $the_query_post->have_posts() ): $the_query_post->the_post(); ?>
    <li class="key-word id-<?php the_ID();?>">
    <a href="<?php echo esc_url( post_permalink() ); ?>" title="<?php the_title();?>">
    <img src="<?php if (empty(get_the_post_thumbnail_url())) {echo ''.bloginfo('template_directory').'/assets/img/logo/not-185x155.jpg' ;} else {the_post_thumbnail_url(array(30, 30));}?>" id="show-img">
    <h3 class="title"><?php the_title();?></h3>
    </a>
    <div class="sub date">Date: <span class="underline"><?php the_date('Y/m/d'); ?></span></div>
    </li>
    <?php endwhile;
    wp_reset_postdata(); 
    echo '</ul>'; 
    else :
    echo '<p class="not-found">Not Fount!</p>';
    endif;
    echo '</div>';
    die();
    }
    
    • This topic was modified 6 years, 9 months ago by buzzztube.
    • This topic was modified 6 years, 9 months ago by buzzztube.

    The page I need help with: [log in to see the link]

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    You are already ordering by title after menu_order. WP_Query does not accept any argument for ordering by content. You can however use the “posts_clauses” filter to directly alter various SQL clauses to meet your needs. There you can add an ORDER clause criteria for the post_content field.

    You can also remove the portion of the search criteria in the WHERE clause that searches post_content so only titles are searched.

    You’ll want to first dump out the passed argument array in order to have an idea of what you have to work with.

Viewing 1 replies (of 1 total)

The topic ‘Search Ajax WordPress Base on Title’ is closed to new replies.