Skip to content

Instantly share code, notes, and snippets.

@petertwise
Last active May 4, 2017 17:25
Show Gist options
  • Save petertwise/479b90ac5bdbf9293aaec10545bcb4c9 to your computer and use it in GitHub Desktop.
Save petertwise/479b90ac5bdbf9293aaec10545bcb4c9 to your computer and use it in GitHub Desktop.
SearchWP support request
<?php
## This is what I want to do - create a straight meta_key ordering for search results - no SearchWP weights involved at all
// Modify the default blog page & archive queries
function squarecandy_modify_main_query( $query ) {
// main page / main blog query
if ( $query->is_home() && $query->is_main_query() ) {
$query->query_vars['posts_per_page'] = -1;
$query->query_vars['orderby'] = 'meta_value_num';
$query->query_vars['meta_key'] = 'order';
$query->query_vars['order'] = 'ASC';
}
// categories
if ( $query->is_category() ) {
$query->query_vars['posts_per_page'] = -1;
$query->query_vars['orderby'] = 'meta_value_num';
$query->query_vars['meta_key'] = 'order';
$query->query_vars['order'] = 'ASC';
}
// search
if ( $query->is_search() ) {
$query->set('post_type', 'post');
$query->set('posts_per_page', -1);
$query->set('orderby', 'meta_value_num');
$query->set('meta_key', 'order');
$query->set('order', 'ASC');
}
}
// Hook my above function to the pre_get_posts action
add_action( 'pre_get_posts', 'squarecandy_modify_main_query' );
## This is one additional SearchWP hook I tried, but it still considers SearchWP Weight to some degree
// Search Order
// https://searchwp.com/docs/kb/using-custom-field-prioritize-search-results/
function my_searchwp_query_main_join( $sql, $engine ) {
global $wpdb;
$my_meta_key = 'order'; // the meta_key you want to order by
$sql = $sql . " LEFT JOIN {$wpdb->postmeta} ON {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id AND {$wpdb->postmeta}.meta_key = '{$my_meta_key}'";
return $sql;
}
add_filter( 'searchwp_query_main_join', 'my_searchwp_query_main_join', 10, 2 );
function my_searchwp_query_orderby( $orderby ) {
global $wpdb;
$my_order = "ASC"; // sort in ascending order
// sort by meta_value, then by SearchWP calculated keyword weight, then by post date
$new_orderby = "ORDER BY {$wpdb->postmeta}.meta_value+0 {$my_order}, " . str_replace( 'ORDER BY', '', $orderby );
return $new_orderby;
}
add_filter( 'searchwp_query_orderby', 'my_searchwp_query_orderby' );
?>
### Begin System Info ###
## Please include this information when posting support requests ##
Multisite: No
SITE_URL: https://bang30dev.bangonacan.org
HOME_URL: https://bang30dev.bangonacan.org
SearchWP Version: 2.8.7
SearchWP License: 91c215f63a5b13cd47f0e269a94d5c87
WordPress Version: 4.7.4
Permalink Structure: /%postname%/
Active Theme: Bang on a Can 30th 1.0.0
Registered Post Stati: publish, future, draft, pending, private, trash, auto-draft, inherit, acf-disabled
PHP Version: 5.6.30
MySQL Version: 5.5.30
Web Server Info: Apache
WordPress Memory Limit: 40M
PHP Safe Mode: No
PHP Memory Limit: 256M
PHP Time Limit: 60
WP_DEBUG: Disabled
WP Table Prefix: Length: 8 Status: Acceptable
WP Remote Post: wp_remote_post() works
TEMPLATES:
search.php Yes
POTENTIAL TEMPLATE CONFLICTS:
NONE
POTENTIAL FILTER CONFLICTS
pre_get_posts => bang30_custom_orderby
pre_get_posts => Hicpo::hicpo_pre_get_posts
pre_get_posts => squarecandy_modify_main_query
pre_get_posts => SearchWP::impose_engine_config
ACTIVE PLUGINS:
Advanced Custom Fields PRO: 5.5.12
AJAX Thumbnail Rebuild: 1.12
Disable Comments: 1.6
Intuitive Custom Post Order: 3.0.8
Post Thumbnail Editor: 2.4.8
SearchWP: 2.8.7
Square Candy TinyMCE Reboot: 1.2
WP All Import - ACF Add-On: 3.1.3
WP All Import Pro: 4.4.4
STATS:
Array
(
[last_activity] => 1 min ago
[total] => 349
[remaining] => 0
[done] => 349
[in_process] =>
)
Main table row count: 17033
Running: No
Processing Purge Queue: No
Paused: No
SETTINGS:
Array
(
[default] => Array
(
[post] => Array
(
[enabled] => 1
[weights] => Array
(
[title] => 20
[content] => 2
[slug] => 10
[tax] => Array
(
[category] => 5
[post_tag] => 5
)
[excerpt] => 6
[cf] => Array
(
[swpp59078a26cf897830355172] => Array
(
[metakey] => composer
[weight] => 15
)
[swpp59078a26cfcf8292139586] => Array
(
[metakey] => location
[weight] => 5
)
[swpp59078a26d0114162706407] => Array
(
[metakey] => performer
[weight] => 15
)
[swpp59078a26d0520584152348] => Array
(
[metakey] => show
[weight] => 5
)
[swpp59078a26d090f968539175] => Array
(
[metakey] => slug
[weight] => 1
)
[swpp59078a26d0cf4550801568] => Array
(
[metakey] => type
[weight] => 1
)
[swpp59078a26d10e0650204166] => Array
(
[metakey] => venue
[weight] => 5
)
[swpp59078a26d14d0616873060] => Array
(
[metakey] => year
[weight] => 5
)
)
)
[options] => Array
(
[exclude] =>
[attribute_to] => 0
)
)
[page] => Array
(
[enabled] =>
[weights] => Array
(
[title] => 20
[content] => 2
[slug] => 10
)
[options] => Array
(
[exclude] =>
[attribute_to] => 0
)
)
[attachment] => Array
(
[enabled] =>
[weights] => Array
(
[title] => 20
[content] => 2
[slug] => 10
[excerpt] => 6
[cf] => Array
(
[swpp59078a26d2731934652151] => Array
(
[metakey] => searchwp_content
[weight] => 2
)
[swpp59078a26d27ad634844275] => Array
(
[metakey] => searchwp_pdf_metadata
[weight] => 2
)
)
)
[options] => Array
(
[exclude] =>
)
)
)
)
PURGE QUEUE:
[Empty]
### End System Info ###
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment