Changing query args with asp_query_args filter

The asp_query_args filter provides a possibility to add/remove/change query arguments right before they are passed to the ajax search pro main query.

This filter is executed after the search options were processed!

Generic usage

add_filter("asp_query_args", "asp_query_args_change", 1, 2);

function asp_query_args_change($args, $search_id) {
  // Do your stuff with the $args array
  // ....  
  // Then return
  return $args;
}

The filter passes two parameters along:

  • $args - the array of the arguments (structure detailed in chapter below)
  • $search_id - the current search ID

Example: Explicitly changing the post types and post fields to search

add_filter("asp_query_args", "asp_query_args_change", 1, 2);

function asp_query_args_change($args, $search_id) {
  // Changing post types to post and page
  $args['post_type'] = array('post', 'page');
  // Search only title and content
  $args['post_fields'] = array('title', 'content');

  return $args;
}

Key list for the $args variable

For the complete $args source list please check: https://gist.github.com/anag0/60f0de34e933ad2e0ac580cc1050ff57

The $args array is the heart and soul of this filter. By changing it's key values you can directly affect the search outcome.

The default values of the key arguments depend on the search intance configuration and the passed arguments from the front-end before the search process.


Generic arguments

's' - Search phrase

The search phrase.

$args['s'] = "some phrase";

'search_type' - Search content types

Determines the search content types. If you wish to return different content types as defined in the search instance option, then this needs to be properly stated within this option.

Type: array, Possible values:

  • cpt -> posts, pages, custom post types
  • taxonomies -> tags, categories and taxonomy terms based on taxonomy slug
  • users -> users
  • blogs -> multisite blog titles
  • buddypress -> buddypress groups or activities
  • comments -> comment results
  • attachments -> file attachments
// Default:
$args['search_type'] = array('cpt');

// Usage:
$args['search_type'] = array('cpt', 'taxonomies');

'engine' - Search engine type

The search engine to use (regular or index). Change this to "index" only if the index table is configured!!

Type: string, Possible values: regular, index

// Default
$args['engine'] = 'regular';

// Changing to index
$args['engine'] = 'index';

'keyword_logic' - Keyword logic

The keyword connection logic which is used for the entered search phrases.

Type: string, Possible values:

  • OR (default) - matches if either of the phrases matches, even partially
  • AND - matches if both phrases match, even partially
  • OREX - matches if either of the phrases matches, only whole words
  • ANDEX - matches if both phrases match, only whole words
// Default
$args['keyword_logic'] = 'OR';

// Force change to AND
$args['keyword_logic'] = 'AND';

Global results limit

The maximum number of results set. If set to 0, then the result type limits are used instead.

If explicitly set to higher than 0, then the results count is distributed evenly for each source.

// Default
$args['limit'] = 0;

// Set to any integer
$args['keyword_logic'] = 50;

Results limit by results type

Only works if the 'limit' argument is set to 0. Defines limits for each source. The '_override' suffixed arguments are for the non-ajax search results.

// Custom post type limits
$args['posts_limit'] = 10;
$args['posts_limit_override'] = 50;
$args['taxonomies_limit']  = 10;
$args['taxonomies_limit_override'] = 20;
$args['users_limit'] = 10;
$args['users_limit_override'] = 20;
$args['blogs_limit'] = 10;
$args['blogs_limit_override'] = 20;
$args['buddypress_limit'] = 10;
$args['buddypress_limit_override'] = 20;
$args['comments_limit'] = 10;
$args['comments_limit_override'] = 20;
$args['attachments_limit'] = 10;
$args['attachments_limit_override'] = 20;

Post & custom post type search related arguments

These arguments affect the post/cpt search.

'post_type' - Post type

Array of post types to search within.

Type: array, Possible values: post, page, ..any registered post type slug

// Default
$args['post_type'] = array('post', 'page');

// Search in product as well
$args['post_type'] = array('post', 'page', 'product');

'post_status' - Post statuses

Array of post statuses

Type: array, Possible values: publish, draft, private, trash, ..any registered custom status

// Default
$args['post_status'] = array('publish');

// Search in drafts as well
$args['post_status'] = array('publish', 'draft');

'post_fields' - Post fields to search in

Array of search fields to search in

Type: array, Possible values: 'title', 'content', 'excerpt', 'terms'

// Default
$args['post_status'] = array('title', 'content', 'excerpt', 'terms');

'post_custom_fields' - Post custom fields to search

Array of custom field name

Type: array, Possible values: any custom field name

// Default
$args['post_custom_fields'] = array();

// Assigning custom field names
$args['post_custom_fields'] = array('custom_field1', 'custom_field2');

'post_in' - Posts by IDs

Limit potential results pool to array of IDs. This only affects the potential result pool, all the other defined criteria must also match!

Type: array, Possible values: existing post IDs

// Default
$args['post_in'] = array();

// Include certain posts
$args['post_in'] = array(1, 2, 3, 4);

'post_not_in' - Posts exclusion by IDs

Explicity exclude IDs from search results

Type: array, Possible values: existing post IDs

// Default
$args['post_not_in'] = array();

// Include certain posts
$args['post_not_in'] = array(5, 6, 7, 8);

Primary and secondary ordering

Type: string, Possible values: 'relevance DESC', 'post_date DESC', 'post_date ASC', 'post_title DESC', 'post_title ASC'

// Default
$args['post_primary_order'] = "relevance DESC";
$args['post_secondary_order'] = "post_date DESC";

'post_tax_filter' - Filter posts by taxonomy terms

Array of taxonomy term rules.

Type: array, Possible values: array of rules

// Default
$args['post_tax_filter'] = array();

// Exclude posts from categories 1,2,3,4 and include from 5,6,7,8
$args['post_tax_filter'] = array(
    array(
        'taxonomy'  => 'category',
        'include'   => array(1, 2, 3, 4), 
        'exclude'   => array(5, 6, 7, 8)
    )
);

// Exclude Posts from certain categories, include from certain tags
$args['post_tax_filter'] = array(
    array(
        'taxonomy'  => 'category',
        'include'   => array(), 
        'exclude'   => array(5, 6, 7, 8)
    ),
    array(
        'taxonomy'  => 'post_tag',
        'include'   => array(10, 11, 23, 44), 
        'exclude'   => array()
    )
);

'post_meta_filter' - Filter posts by post meta (custom fields)

Array of custom field rules.

Type: array, Possible values: array of rules

// Default
$args['post_meta_filter'] = array();

// Usage with operators
$args['post_meta_filter'] = array(
    array(
        'key'     => 'age',         // meta key
        'value'   => array( 3, 4 ), // mixed|array
         // @param string|array compare
         // Numeric Operators
         //      '<' -> less than
         //      '>' -> more than
         //      '<>' -> not equals
         //      '=' -> equals
         //      'BETWEEN' -> between two values
         // String Operators
         //      'LIKE'
         //      'NOT LIKE'
         //
        'operator' => 'BETWEEN'
    )
    // .. additional rules ..
);

'post_date_filter' - Filter posts by dates

Array of date rules.

Type: array, Possible values: array of rules

// Default
$args['post_date_filter'] = array();

// By year, month, day given separately
$args['post_date_filter'] = array(
    array(
        'year'  => 2015,            // year, month, day ...
        'month' => 6,
        'day'   => 1,
        'operator' => 'include',    // include|exclude
        'interval' => 'before'      // before|after
    )
);

// By given y-m-d format
$args['post_date_filter'] = array(
    array(
        'date'  => "2015-06-01",     // .. or date parameter in y-m-d format
        'operator' => 'include',    // include|exclude
        'interval' => 'before'      // before|after
    )
);

'post_user_filter' - Filter by author (user)

Array of user rules.

Type: array, Possible values: array of rules

// Default
$args['post_user_filter'] = array();

// Include/Exclude by user IDs
$args['post_user_filter'] = array(
    'include' => (1, 2, 3, 4),  // include by User IDs
    'exclude' => (5, 6, 7, 8)   // exclude by User IDs
);

Before adjusting the settings, enable attachment search explicitly:

if ( !in_array('attachments', $args['search_type']) )
    $args['search_type'][] = 'attachments';

Default attachment search arguments:

// Defaults
$args['attachments_search_title']  = true;
$args['attachments_search_content'] = true;
$args['attachments_search_caption'] = true;
$args['attachments_search_terms'] = false;
// Use attachment as image if it is an image type
$args['attachment_use_image'] = true;
$args['attachment_mime_types'] = array(
  'image/jpeg', 
  'image/gif',
  'image/png',
  'image/tiff',
  'image/x-icon'
);
// Exclude attachments by ID
$args['attachment_exclude']    = array();

For the possible mime types, see the mime types table on the attachment search documentation page.

Before adjusting the settings, enable buddypress search explicitly:

if ( !in_array('buddypress', $args['search_type']) )
    $args['search_type'][] = 'buddypress';

Default arguments:

// BuddyPress default arguments
// Search in groups
$args['bp_groups_search']          = false,
// Search in public groups
$args['bp_groups_search_public']   = true;
// Search in private groups
$args['bp_groups_search_private']  = true;
// Search in hidden groups 
$args['bp_groups_search_hidden']   = true;
// Search in user activities
$args['bp_activities_search']      = true;

Taxonomy Term (category) search arguments

Before adjusting the settings, enable taxonomies search explicitly:

if ( !in_array('taxonomies', $args['search_type']) )
    $args['search_type'][] = 'taxonomies';

Default arguments:

$args['taxonomy_include'] = array("category", "post_tag"); // taxonomies to search for terms
$args['taxonomy_terms_exclude'] = array();     // terms to exclude by ID
$args['taxonomy_terms_search_description'] = true;
$args['_taxonomy_posts_affected'] => true;     // Display the number of posts affected

Before adjusting the settings, enable users search explicitly:

if ( !in_array('users', $args['search_type']) )
    $args['search_type'][] = 'users';

Default arguments:

$args['user_login_search'] = true;
$args['user_display_name_search'] = true;
$args['user_first_name_search'] = true;
$args['user_last_name_search'] = true;
$args['user_bio_search'] = true;

// Array of meta fields
$args['user_search_meta_fields'] = array();

// Array of buddypress fields
$args['user_search_bp_fields'] = array();

// Array of roles to exclude
$args['user_search_exclude_roles'] = array();

results matching ""

    No results matching ""