ASP_Query
ASP_Query class usage to fetch results with Ajax Search Pro
The ASP_Query class is a wrapper to access Ajax Search Pro search features programmatically in your own code.
The full class name, with arguments explained can be found in wp-content/plugins/ajax-search-pro/includes/classes/search/class-asp-query.php file

Usage

1
$asp_query = new ASP_Query();
2
foreach ( $asp_query->posts as $result ) {
3
/**
4
* $result (stdClass object) {
5
* -> Same properties as in WP_Post +
6
* 'asp_data' (stdClass object) {
7
* Properties always present:
8
* 'id' -> Post or other result object (taxonomy term, user etc..) ID,
9
* 'title' -> Result title
10
* 'content' -> Result content
11
* 'image' -> Result image URL
12
* 'post_type' -> Result post type (if available)
13
* 'content_type' -> Content type (pagepost, user, term, attachment etc..)
14
* 'author' -> Author display name (empty string where does not apply)
15
* 'blogid' -> Blog ID (for multisites)
16
* Optional properties
17
* 'post_mime_type' -> (for media results only) Mime type
18
* 'taxonomy' -> (for term results only) Taxonomy name
19
* 'user_login' -> (for user results only) User login name
20
* 'user_nicename' -> (for user results only) User nicename
21
* 'user_display_name' -> (for user results only) User display_name
22
* }
23
* }
24
**/
25
// do your thing
26
var_dump($result);
27
}
Copied!
The class constructor accepts two optional parameters, the arguments, and the search ID
1
$asp_query = new ASP_Query( array $args, int $search_id );
Copied!
  • $args (array) - array of search arguments, for the possible arguments please check the asp_query_args documentation
  • $search_id (int) - the relevant search instance ID. When set, the plugin will fetch the search instance default configuration and use them as the default query arguments. The $args argument can still override this default configuration if needed.

Example uses

For the possible arguments please check the asp_query_args documentation

Generic search by default configuration, without search instance

1
$args = array(
2
's' => 'test'
3
);
4
$asp_query = new ASP_Query($args);
5
foreach ( $asp_query->posts as $result ) {
6
// do your thing
7
}
Copied!

Search based on configuration of search instance = 1

1
$args = array(
2
's' => 'test'
3
);
4
$asp_query = new ASP_Query($args, 1);
5
foreach ( $asp_query->posts as $result ) {
6
// do your thing
7
}
Copied!

Generic search with more arguments

1
$args = array(
2
"s" => $phrase,
3
"_ajax_search" => false,
4
'keyword_logic' => 'AND',
5
'secondary_logic' => 'OR',
6
"posts_per_page" => 20,
7
'post_type' => $ptypes,
8
'post_status' => array('publish', 'future', 'pending', 'private'),
9
'post_fields' => array(
10
'title', 'ids'
11
)
12
);
13
$asp_query = new ASP_Query();
14
foreach ( $asp_query->posts as $result ) {
15
// do your thing
16
}
Copied!
Copy link