By default, WordPress post titles are sorted by the date they were published. This means that if you have thousands of WordPress posts that contain “tips” in the title tag, and you search for "tips" in the WordPress search box, the ones that will be shown at the top of the results are the most recent articles. All search queries from WordPress search forms are handled by a special WordPress file called “query.php.” This can be found in the wp-includes folder in WordPress's FTP files. In this PHP file (query.php), the most important section in the source code is this one: /////////////////////////////////////////// if ( empty($q['order']) || ((strtoupper($q['order']) != 'ASC') && (strtoupper($q['order']) != 'DESC')) ) $q['order'] = 'DESC'; // Order by if ( empty($q['orderby']) ) { $q['orderby'] = "$wpdb->posts.post_date ".$q['order']; } else { // Used to filter values $allowed_keys = array('author', 'date', 'category', 'title', 'modified', 'menu_order', 'parent', 'ID', 'rand'); $q['orderby'] = urldecode($q['orderby']); $q['orderby'] = addslashes_gpc($q['orderby']); $orderby_array = explode(' ',$q['orderby']); if ( empty($orderby_array) ) $orderby_array[] = $q['orderby']; $q['orderby'] = ''; for ($i = 0; $i < count($orderby_array); $i++) { // Only allow certain values for safety $orderby = $orderby_array[$i]; switch ($orderby) { case 'menu_order': break; case 'ID': $orderby = "$wpdb->posts.ID"; break; case 'rand': $orderby = 'RAND()'; break; default: $orderby = "$wpdb->posts.post_" . $orderby; } if ( in_array($orderby_array[$i], $allowed_keys) ) $q['orderby'] .= (($i == 0) ? '' : ',') . $orderby; } // append ASC or DESC at the end if ( !empty($q['orderby'])) $q['orderby'] .= " {$q['order']}"; if ( empty($q['orderby']) ) $q['orderby'] = "$wpdb->posts.post_date ".$q['order']; } /////////////////////////////////////////////////////// This is where WordPress will know whether the search results will be sorted by post date or other parameters. This is the section where the results will be sorted, whether in descending or ascending order. Specifically (in bold): if ( empty($q['order']) || ((strtoupper($q['order']) != 'ASC') && (strtoupper($q['order']) != 'DESC')) ) $q['order'] = 'DESC'; is responsible for a descending post publishing date order in the default search results; that is, the most recent post should be shown at the top of the search results list. $q['orderby'] = "$wpdb->posts. post_date".$q['order']; This piece of code shows that the search results will be based on “post_date.”
blog comments powered by Disqus |
|
|
|
|
|
|
|