Ovifdiu COmrasdd
Ovifdiu COmrasdd

Reputation: 55

Wordpress Search Code

Well so i have a searchform.php and a search.php and under the search.php fille i have the following code:

<?php if (have_posts()) : ?>
<?php
    while (have_posts()) : the_post(); ?>
         <div class="row">
            <div class="col-md-12">
               <h1><a href="<?php echo get_post_meta($post->ID, 'linkcautare', true); ?>"> <?php the_title();?> </a></h1>
            </div>
         </div>
         <div class="row">
            <div class="col-md-12">
               <p><?php the_excerpt(); ?></p>

            </div>
         </div>
    <?php endwhile;

    else :
        echo '<p>No content found!</p>';

    endif; ?>

      </div>
   </div>

The question is how can i list the posts by sowing the most viewed on top?

Upvotes: 0

Views: 62

Answers (1)

Dreamm
Dreamm

Reputation: 141

add_filter('posts_orderby','my_sort_custom',10,2);
function my_sort_custom( $orderby, $query ){
    global $wpdb;

    if(!is_admin() && is_search())
        $orderby =  $wpdb->prefix."posts.post_type DESC, {$wpdb->prefix}posts.post_date DESC";

    return  $orderby;
}

this is my way change by post type & post date

change query if you have number of page views

Upvotes: 1

Related Questions