# SearchQuery

The **WPDRMS\ASP\Query\SearchQuery()** class is a wrapper to access Ajax Search Pro search features programmatically in your own code.

### Usage

```php
$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

```php
$asp_query = new WPDRMS\ASP\Query\SearchQuery( array $args, int $search_id );
```

* **$args** *(*&#x61;rra&#x79;*)* - array of search arguments, for the possible arguments please check the [*asp\_query\_args* documentation](/hooks/filters/query-and-output/asp_query_args.md)
* **$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](/hooks/filters/query-and-output/asp_query_args.md)

#### Generic search by default configuration, without search instance

```php
$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&#x20;

```php
$args = array(
	's' => 'test'
);
$asp_query = new WPDRMS\ASP\Query\SearchQuery($args, 1);
foreach ( $asp_query->posts as $result ) {
	// do your thing
}
```

#### Generic search with more arguments

```php
$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 WPDRMS\ASP\Query\SearchQuery();
foreach ( $asp_query->posts as $result ) {
	// do your thing
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://knowledgebase.ajaxsearchpro.com/api/searchquery.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
