Support » Plugin: Dave's WordPress Live Search » Filter to alter results in functions.php

  • I’ve seen this question before on here, but never could find a direct answer. I have a filter to modify to results of my regular search using this:

    add_filter( 'pre_get_posts', 'dontShowOffs' );

    The script is named “dontShowOffs”. I’ve tried to do this to alter the live search results:

    add_filter( 'dwls_alter_results', 'dontShowOffs');

    But this isn’t working. What is the syntax or how do I apply my dontShowOffs script to the live search results in my functions.php file?

    Thanks!

    https://wordpress.org/plugins/daves-wordpress-live-search/

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Dana Ross

    (@csixty4)

    pre_get_posts passes a complete WP_Query object to the filter function. dwls_alter_results just passes an array of posts it gets by calling $wp_query->get_posts().

    I don’t know what your dontShowOffs function looks like, so I can’t help you adapt it do work with DWLS. But if it’s calling get_posts() to retrieve an array of posts, that’s what my plugin is already passing to it.

    Thread Starter ahigginbotham

    (@ahigginbotham)

    Basically my function reads custom fields to determine if an event status is 1,2 or 3. I only want Live Search to pull those with a status of 1, AND that is later than the current date. Here’s my complete dontShowOffs function:

    function dontShowOffs($query) {
    	global $wpdb;
    	if ( !is_admin() && $query->is_main_query() ) {
    		$inactiveevents = $wpdb->get_col("SELECT post_id FROM $wpdb->postmeta WHERE meta_key='wpcf-event-status' AND meta_value = 3");
    		$currentDate = getdate();
    		$offevents = array();
    		$pastevents = array();
    		if ( !is_admin() && $query->is_search() ) {
    			$offevents = $wpdb->get_col("SELECT post_id FROM $wpdb->postmeta WHERE meta_key='wpcf-event-status' AND meta_value = 2");
    			$pastevents = $wpdb->get_col("SELECT post_id FROM $wpdb->postmeta WHERE meta_key='wpcf-start-date' AND meta_value < $currentDate[0]");
    		}
    
    		$soloexcludes = array(38);
    		$dontinclude = array_merge($inactiveevents, $soloexcludes, $offevents, $pastevents);
    		$query->set('post__not_in', $dontinclude);
    		$query->set('orderby', 'type');
    		$query->set('order', 'DESC' );
    	}
    	return $query;
    }

    If you could help me figure this out if would be greatly appreciated. Thanks!

    Thread Starter ahigginbotham

    (@ahigginbotham)

    Hi Dave,

    Just following up on this as the issue still remains. Do you have any insights?

    Thanks,
    Anthony

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Filter to alter results in functions.php’ is closed to new replies.