94

How to Display Elements Based on Search Results in Oxygen Builder

Powered by WPCodeBox

This Oxygen Builder condition allows you to display elements based on search query results, perfect for showing suggestions or content in search results templates when a query returns no results.


<?php

add_action('plugins_loaded', function () {

    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('=='), 'search_has_results_callback', 'Search');

        function search_has_results_callback($value, $operator)
        {

            global $wp_query;
            $posts_found = $wp_query->found_posts;

            if ($value == "true" && $posts_found > 0) {
                return true;
            } else if ($value == "false" && $posts_found == 0) {
                return true;
            }

        }

    }
});

Other Snippets

WPCodeBox is a WordPress Code Snippets Manager that allows you to share your WordPress Code Snippets across your sites.