Skip to content

Instantly share code, notes, and snippets.

@csaborio001
Created March 27, 2021 03:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save csaborio001/e1e4847490ea5ad8ee780577fbc78111 to your computer and use it in GitHub Desktop.
Save csaborio001/e1e4847490ea5ad8ee780577fbc78111 to your computer and use it in GitHub Desktop.
<?php
/**
* Oxygen helper class to define when to display search results.
*
* @package scorpiotek-wordpress-utilities
*/
namespace ScorpioTek\WPUtil\Oxygen;
class SearchAssist {
/**
* Initialises the condtition to show up in Oxygen's interface and assigns
* the callback function.
*/
public function __construct() {
if( function_exists( 'oxygen_vsb_register_condition' ) ) {
global $oxy_condition_operators;
oxygen_vsb_register_condition(
'Has Results',
array(
'options' => array(
'true',
'false',
),
'custom' => false,
),
array(
'==',
),
array(
$this,
'search_has_results_callback',
),
'Search'
);
}
}
/**
* Callback function to figure out if the search yielded any results.
*
* @param mixed $value - the value of the search, only false if nothing was found.
* @param mixed $operator - the operator to use for comparison.
* @return void
*/
function search_has_results_callback( $value, $operator ) {
global $wp_query;
$posts_found = $wp_query->found_posts;
if ( 'true' === $value && $posts_found > 0 ) {
return true;
} elseif( 'false' === $value && 0 === $posts_found ) {
return true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment