Ajax Search Pro Knowledge Base
Buy Ajax Search Pro!DocumentationGet SupportDevelopment
  • Knowledge Base
  • Safe Coding Guideline
  • Hooks
    • Filters
      • Query & Output
        • asp_query_args
        • asp_query_{type}
        • asp_cached_content
        • asp_pre_get_front_filters
        • asp_before_ajax_output
        • asp_shortcode_output
        • asp_print_search_query
      • Keyword Suggestions
      • Search Results
        • asp_suggested_phrases
        • asp_results
        • asp_cpt_results
        • asp_buddyp_results
        • asp_attachment_results
        • asp_comment_results
        • asp_blog_results
        • asp_terms_results
        • asp_peepso_group_results
        • asp_peepso_activities_results
        • asp_only_keyword_results
        • asp_only_non_keyword_results
        • asp_result_groups
      • CSS & JS
        • asp_load_js
        • asp_load_css
        • asp_load_css_js
      • Template & Output
        • asp_icl_t
      • Index Table Related
        • asp_index_on_save_stop
        • asp_indexing_keywords
        • asp_indexing_string_pre_process
        • asp_indexing_string_post_process
        • asp_post_content_before_tokenize_clear
        • asp_post_content_before_tokenize
        • asp_post_excerpt_before_tokenize
        • asp_post_title_before_tokenize
        • asp_file_contents_before_tokenize
        • asp_post_permalink_before_tokenize
        • asp_index_terms
        • asp_post_custom_field_before_tokenize
        • asp_index_cf_contents_before_tokenize
        • asp_index_before_shortcode_execution
        • asp_index_after_shortcode_execution
    • Templating
      • Filter layouts Templating
      • Result Templating
    • Constants
  • Frontend Filters
    • Taxonomy Filters
      • Restricting results to the same category as the current post object
      • How to automatically check/select filter values based on the archive page?
    • Frontend filters API
  • Tips & Miscellaneous
    • Divi
      • Divi Blogs Live Search and Filter
    • Jet Engine
      • Jet Engine Listing Grid Live Search and Filter
      • Searching Jet Engine Custom Meta Storage fields
    • Tutorials
      • PDF results thumbnails
      • Demo setup: Staff search and Filter
      • Demo Setup: WooCommerce Search
      • Demo setup: WooCommerce Shop Search and Filter
      • Demo Setup: Events Search – Events Manager
      • Demo Setup: Events Search – The Events Calendar
      • Compact ‘pop-out’ search bar placement on specific pages only
      • Index Table – Indexing ACF repeater field titles and contents
      • Change Suggested Phrases conditionally
      • How to add shortcode to the results content?
      • How to add variables to the “redirect to url” or the “show more url”?
      • Indexing Shortcodes within custom field contents
    • Post Types
      • Index Table - Indexing child post contents to parent
      • Limit results to specific post IDs only
      • Filter posts (or CPT) which user can’t access
      • Restricting results by user Groups using the Groups plugin by itthinx
      • Limiting results to specific posts by parent ID
      • Limit results to current page children
      • Excluding posts or CPT by parent ID(s)
      • Searching posts, pages (or any CPT) by specified keywords only, nothing else
      • Showing the post type name in result title or content
      • Searching within given categories/taxonomy terms only
      • Search only in the same category as the current post or page (single page)
      • Search only within the current category (or any taxonomy) archive
      • Filtering pages by page template
    • Taxonomy Terms
      • Displaying taxonomy name in taxonomy term results
      • Limiting taxonomy term results to specific term IDs only
    • WooCommerce
      • Displaying On Sale products only in WooCommerce
      • Ordering product by stock status
      • Making a product in-stock & out of stock filter
      • Add to cart button for Vertical and Horizontal results
      • Get formatted price in result title or in content
      • Showing products in-stock only
      • Showing in-stock and backorder products only
      • Excluding hidden catalog products
      • Showing featured products first
      • How to search products & product SKU?
      • How to search Products by variation SKUs?
      • Search product attributes
      • How to search users by city, state, zip code… ?
    • Compatibility
      • WPML Compatibility issues and fixes
    • Files & Media
      • Searching Image EXIF metadata
    • Other
      • Restrict results from the current author archive page
      • Presetting search options via a custom URL
      • Matomo analytics tracking integration
      • Replace search keywords (whole words)
      • Replace or remove characters from search phrase
      • Adding spaces in search phrase between alphabetics and numbers
      • How to change the results URL to something else?
      • Numbering the results
      • How to use the search without the live ajax feature, as a regular search?
      • Empty search input field on the search results page
      • Singular and Plural keywords index
  • Other
    • SearchQuery
    • REST API
    • Building a custom REST API
    • Javascript API
    • Javascript Hooks
      • asp_redirect_url
      • asp_search_data
      • asp_live_load_html
      • asp_search_html
      • asp_compact_width
    • Theme Functions
Powered by GitBook
On this page
  • Parameters
  • Accepted Properties of the $args object
  • Generic arguments
  • 's' - Search phrase
  • 'search_type' - Search content types
  • 'engine' - Search engine type
  • 'keyword_logic' - Keyword logic
  • Global results limit
  • Results limit by results type
  • Post & custom post type search related arguments
  • 'post_type' - Post type
  • 'post_status' - Post statuses
  • 'post_fields' - Post fields to search in
  • 'post_custom_fields' - Post custom fields to search
  • 'post_in' - Posts by IDs
  • 'post_not_in' - Posts exclusion by IDs
  • Primary and secondary ordering
  • 'post_tax_filter' - Filter posts by taxonomy terms
  • 'post_meta_filter' - Filter posts by post meta (custom fields)
  • 'post_date_filter' - Filter posts by dates
  • 'post_user_filter' - Filter by author (user)
  • Attachment search related arguments
  • BuddyPress search related arguments
  • Taxonomy Term (category) search arguments
  • User search related arguments
  • Special arguments
  • Allowing missing translations in results when using WPML
  1. Hooks
  2. Filters
  3. Query & Output

asp_query_args

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!

// Classic Usage
add_filter("asp_query_args", "asp_query_args_change", 10, 2);
function asp_query_args_change($args, $search_id) {
  // Do your stuff with the $args array
  // ....  
  // Then return
  return $args;
}

// Shorter version with type safety and anonymous function
use WPDRMS\ASP\Models\SearchQueryArgs;
add_filter("asp_query_args", function(SearchQueryArgs $args, int $search_id) {
  return $args;
}, 10, 2);

Parameters

  • $args (SearchQueryArgs) - the object of the arguments (structure detailed in chapter below). This object implements the ArrayAccess interface in a way that all of it's properties are accessible as array keys as well.

  • $search_id (int) - the current search ID

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

// Classic Way
add_filter("asp_query_args", "asp_query_args_change", 10, 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;
}

// Shorter version with type safety and anonymous function
use WPDRMS\ASP\Models\SearchQueryArgs;
add_filter("asp_query_args", function(SearchQueryArgs $args, int $search_id) {
  $args->post_type = array('post', 'page');
  $args->post_fields = array('title', 'content');
  return $args;
}, 10, 2);

Accepted Properties of the $args object

For the complete $args source list with type hints and accepted values please check: wp-content/plugins/ajax-search-pro/includes/classes/Models/SearchQueryArgs.php

The $args object 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.

It is recommended to treat the $args variable as SearchQueryArgs object instad of an array to get type hints in your editor.

Generic arguments

's' - Search phrase

The search phrase.

$args->s = "some phrase";

// Or the classic way
$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),
        'allow_empty' => true // Allow results, that does not have connection with this taxonomy
    )
);

// 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 ), // int|float|string|array|timestamp|datetime
         // @param string|array compare
         // Numeric Operators, also used for timestamp value
         //      '<' -> less than
         //      '>' -> more than
         //      '<>' -> not equals
         //      '=' -> equals
         //      'BETWEEN' -> between two values
	 // Date Operators for datetime values e.g. "2020-03-24 17:45:12"
	 //	 !!NOTE!!: The value has to be in datetime format including the time e.g. "2020-03-24 17:45:12"
	 // 	 'datetime =' -> date equals (that day)
	 //	 'datetime <>' -> date does not equal (that day)
	 //	 'datetime <'  -> date before (that day)
	 //	 'datetime <='  -> date before including (that day)
	 //	 'datetime >'  -> date after (that day)
	 //	 'datetime >='  -> date after including (that day)
         // String Operators
         //      'LIKE'
         //      'NOT LIKE'
         //      'IN'
        'operator' => 'BETWEEN',
        'allow_missing' => false   // allow match if this custom field is unset
    )
    // .. 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
);

Attachment search related arguments

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.

BuddyPress search related arguments

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

User search related arguments

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();

Special arguments

Allowing missing translations in results when using WPML

If the site language is used, the translation can be non-existent if not marked with a language explicitly. This allows excluding those results if needed.

$args['_wpml_allow_missing_translations']= true; //default true
PreviousQuery & OutputNextasp_query_{type}

Last updated 11 months ago