Jump to content


Photo

How to sort search result by date


  • Please log in to reply
4 replies to this topic

#1 Sergei-78

Sergei-78
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 27 January 2017 - 10:58 AM

Please could you help me to sort out the search result? I need to sort it by date and the latest post should be at the top.

<?php if ( have_posts() ): ?>
<h2 style="font-weight:normal!important">Search Results for '<?php echo get_search_query(); ?>'</h2> 
<ol style="list-style:none!important;">
<?php while ( have_posts() ) : the_post(); ?>
<li>
<article>
<h2><a href="<?php esc_url( the_permalink() ); ?>" title="Permalink to <?php the_title(); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
<time datetime="<?php the_time( 'Y-m-d' ); ?>" pubdate><?php the_date(); ?> <?php the_time(); ?></time> <?php comments_popup_link('Leave a Comment', '1 Comment', '% Comments'); ?>
<?php the_content(); ?>
</article>
</li>
<?php endwhile; ?>
</ol>
<?php else: ?>
<h2>No results found for '<?php echo get_search_query(); ?>'</h2>
<?php endif; ?>


#2 ginerjm

ginerjm
  • Members
  • PipPipPip
  • Handball player
  • 3,398 posts
  • LocationVoorheesville NY

Posted 27 January 2017 - 10:15 PM

Your code is spaghetti and thus very hard to read, let alone interpret. Where is the actual query that produces the results that you want to sort differently? Have you tried altering the 'order by' clause of your select statement?

You should really learn to structure your script so as to place the logic (php) at the top and produce php vars of dynamic data and then place those vars into a cohesive block of html code at the bottom. Makes for better maintenance and readability. That way you can write cleaner lines without going in & out of hph mode all the time.

One should also write code one statement at a time and not consolidate things into single lines. Again - readability.
JG
PS - If you're posting here you should be using:
        error_reporting(E_ALL);
        ini_set('display_errors', '1');

at the top of ALL php code while you develop it!

#3 maxxd

maxxd
  • Members
  • PipPipPip
  • Advanced Member
  • 693 posts
  • LocationNorth Carolina

Posted 31 January 2017 - 01:17 PM

Try the posts_orderby hook to alter the ORDER BY clause of the main query. More information here. You'll want to use is_search() to make sure you're dealing with the search query only.

 

ginerjm, unfortunately this is the standard way of writing a template for WordPress. There are other ways out there (one of my favorites - Timber - allows the use of Twig templates), but they're non-standard and all depend on either a plugin or specific theme.



#4 ginerjm

ginerjm
  • Members
  • PipPipPip
  • Handball player
  • 3,398 posts
  • LocationVoorheesville NY

Posted 31 January 2017 - 01:44 PM

And so I learn....

Would be nice if people mentioned WP in their post...
JG
PS - If you're posting here you should be using:
        error_reporting(E_ALL);
        ini_set('display_errors', '1');

at the top of ALL php code while you develop it!

#5 maxxd

maxxd
  • Members
  • PipPipPip
  • Advanced Member
  • 693 posts
  • LocationNorth Carolina

Posted 31 January 2017 - 01:57 PM

Would be nice if people mentioned WP in their post...

 

True. Unfortunately, I think some newer folk don't know that what they're seeing is WP-specific code and not standard PHP.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users