Support » Plugin: WP-PostViews » Sorting when searching for posts by views

  • Hi!
    Tell me, how can I make the output of posts by views through a search with a filter?

    <form action="<?php echo esc_url( home_url( '/' ) ); ?>" method="get">
    
    <input type="text" value="" name="s" id="s" placeholder="Search...">
    
    <select name="orderby">
     <option value="views">views</option>
     <option value="date">date</option>
    </select>
    
    <input type="radio" name="order" value="DESC" checked>DESC
    <input type="radio" name="order" value="ASC">ASC 
    
    <div><input type="submit" value="Search"></div>
    <input type="hidden" name="meta_key" value="views">
    </form>
    
    <?php
    $args = array(
     'post_type' => 'post',
     's' => $_GET['s'],
     'order' => $_GET['order'],
     'meta_key' => $_GET['meta_key'],
     'orderby' => $_GET['orderby'],
    );
    
    if($_GET['orderby'] == 'views'){
     $args['meta_key'] = 'views';
     $args['orderby'] = 'meta_value_num';
    };
    
    query_posts($args);
    if ( have_posts() ) : while ( have_posts() ) : the_post();
    // posts
    endwhile; endif;
    ?>

    Guest writes a keyword, selects sort by views and ascending.
    After clicking the submit button Search, display posts by views on the search page.

    Posts are published only by date, after search

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

  • The topic ‘Sorting when searching for posts by views’ is closed to new replies.