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
$asp_query = new ASP_Query();
foreach ( $asp_query->posts as $result ) {
/**
* $result (stdClass object) {
* -> Same properties as in WP_Post +
* 'asp_data' (stdClass object) {
* Properties always present:
* 'id' -> Post or other result object (taxonomy term, user etc..) ID,
* 'title' -> Result title
* 'content' -> Result content
* 'image' -> Result image URL
* 'post_type' -> Result post type (if available)
* 'content_type' -> Content type (pagepost, user, term, attachment etc..)
* 'author' -> Author display name (empty string where does not apply)
* 'blogid' -> Blog ID (for multisites)
* Optional properties
* 'post_mime_type' -> (for media results only) Mime type
* 'taxonomy' -> (for term results only) Taxonomy name
* 'user_login' -> (for user results only) User login name
* 'user_nicename' -> (for user results only) User nicename
* 'user_display_name' -> (for user results only) User display_name
* }
* }
**/
// do your thing
var_dump($result);
}
The class constructor accepts two optional parameters, the arguments, and the search ID
$asp_query = new ASP_Query( array $args, int $search_id );
- $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.
$args = array(
's' => 'test'
);
$asp_query = new ASP_Query($args);
foreach ( $asp_query->posts as $result ) {
// do your thing
}
$args = array(
's' => 'test'
);
$asp_query = new ASP_Query($args, 1);
foreach ( $asp_query->posts as $result ) {
// do your thing
}
$args = array(
"s" => $phrase,
"_ajax_search" => false,
'keyword_logic' => 'AND',
'secondary_logic' => 'OR',
"posts_per_page" => 20,
'post_type' => $ptypes,
'post_status' => array('publish', 'future', 'pending', 'private'),
'post_fields' => array(
'title', 'ids'
)
);
$asp_query = new ASP_Query();
foreach ( $asp_query->posts as $result ) {
// do your thing
}
Last modified 1yr ago