SearchQuery class usage to fetch results with Ajax Search Pro
The WPDRMS\ASP\Query\SearchQuery() class is a wrapper to access Ajax Search Pro search features programmatically in your own code.
Usage
$asp_query = new WPDRMS\ASP\Query\SearchQuery();
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 WPDRMS\ASP\Query\SearchQuery( array $args, int $search_id );
$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
Generic search by default configuration, without search instance
$args = array(
's' => 'test'
);
$asp_query = new WPDRMS\ASP\Query\SearchQuery($args);
foreach ( $asp_query->posts as $result ) {
// do your thing
}
Search based on configuration of search instance = 1
$args = array(
's' => 'test'
);
$asp_query = new WPDRMS\ASP\Query\SearchQuery($args, 1);
foreach ( $asp_query->posts as $result ) {
// do your thing
}