class Query extends BaseQuery

Class Query

Properties

$subQuery from  BaseQuery
$operation
$_query

Methods

__construct(string $data = '', string $type = '')

Query Class to initalize your Query

JSON
toJSON()

To transform the Result object to server response content

Query
except(string $level = 'BASE', array $field_uids = array())

To exclude the fields from the result set

Query|Entry
only(string $level = 'BASE', array $field_uids = array())

To project the fields in the result set

Query
includeReference($field_uids = array())

To include reference(s) of other content type in entries

Query
search($search = '') deprecated

To search the given string in the entries

Query
regex()

To perform the regular expression test on the specified field

Query
logicalAND()

Logical AND queries are pushed

Query
logicalOR()

Logical OR queries are pushed

Query
ascending($field_uid = '')

To sort the entries in ascending order of the specified field

Query
descending($field_uid = '')

To sort the entries in descending order of the specified field

Query
notExists($field_uid = '')

To check field doesn't exists

Query
exists($field_uid = '')

To check field exists

Query
includeFallback()

To include fallback content if specified locale content is not publish.

Query
includeBranch()

To include branch of publish content.

Query
includeSchema() deprecated

To include schema along with entries

Query
includeReferenceContentTypeUID()

This method includes the content type UIDs of the referenced entries returned in the response.

Query
includeContentType()

To include content_type along with entries

Query
includeEmbeddedItems()

To include Embedded Items along with entries

Query
includeCount()

To include the count of entries based on the results

Query
count()

To get only count result

Query
includeOwner()

To include the owner of entries based on the results

Query
addParam(string $key = '', string $value = '')

To add query parameter in query

Query
language($lang = '')

To set the language code in the query

Query
skip(int $skip = 0)

Skip the specified number of entries from result set

Query
tags(array $tags = array())

Result set entries should have tags specified

Query
limit(int $limit = '')

Limit the specified number of entries from result set

Query
containedIn(string $field = '', array $value = array())

Query the field value from the given set of values

Query
notContainedIn(string $field = '', array $value = array())

Query the field value other than the given set of values

Query
where(string $key = '', string $value = '')

Query the field which has exact value as specified

Query
lessThan(string $field = '', string $value = '')

Query the field which has less value than specified one

Query
lessThanEqualTo(string $field = '', string $value = '')

Query the field which has less or equal value than specified one

Query
greaterThan(string $field = '', string $value = '')

Query the field which has greater value than specified one

Query
greaterThanEqualTo(string $field = '', string $value = '')

Query the field which has greater or equal value than specified one

Query
notEqualTo(string $field = '', string $value = '')

Query the field which has not equal to value than specified one

Query
addQuery(array $_query = array())

Add Query is used to add the raw/array query to filter the entries

query
getQuery()

Get the raw/array query from the current instance of Query/Entry

Request
find()

Get all entries based on the specified subquery

Request
findOne() deprecated

Get single entry based on the specified subquery

Details

at line 43
__construct(string $data = '', string $type = '')

Query Class to initalize your Query

Parameters

string $data
  • data for query
string $type
  • type of query

in BaseQuery at line 76
JSON toJSON()

To transform the Result object to server response content

Return Value

JSON

Examples

//Converting response array to JSON format

use Contentstack\Contentstack;
$stack = Contentstack::Stack("API_KEY", "DELIVERY_TOKEN", "ENVIRONMENT");
$result = $stack->ContentType('CONTENT_TYPE_UID')->Query()->toJSON()->find();

in BaseQuery at line 100
Query except(string $level = 'BASE', array $field_uids = array())

To exclude the fields from the result set

Parameters

string $level -
array $field_uids
  • field uids as array

Return Value

Query

Examples

In the Product content type, if we need to retrieve the data of entries of all the
other fields except the Price in USD parameter, you can send the parameter as:

except(string $level = 'BASE', array $field_uids = array())

use Contentstack\Contentstack;
$stack = Contentstack::Stack("API_KEY", "DELIVERY_TOKEN", "ENVIRONMENT");
$result = $stack->ContentType('product')->Entry('CONTENTTYPE_UID')->toJSON()->except('BASE',array('price'))->fetch();

in BaseQuery at line 132
Query|Entry only(string $level = 'BASE', array $field_uids = array())

To project the fields in the result set

Parameters

string $level -
array $field_uids
  • field uids as array

Return Value

Query|Entry

Examples

In the Product content type, if we need to retrieve the data of only the Price in USD
parameter of all the entries, you can send the parameter as:

only(string $level = 'BASE', array $field_uids = array())

use Contentstack\Contentstack;
$stack = Contentstack::Stack("API_KEY", "DELIVERY_TOKEN", "ENVIRONMENT");
$result = $stack->ContentType('product')->Entry(' ')->toJSON()->only('BASE',array('price'))->fetch();

in BaseQuery at line 166
Query includeReference($field_uids = array())

To include reference(s) of other content type in entries

Parameters

$field_uids
  • array of reference field uids

Return Value

Query

Examples

//In the Product content type, there is a reference field called Categories, which refers entries of another content type. Let’s assume that you had
created an entry for the Product content type, and the value selected in the Categories field was ‘Mobiles’. If you fetch the entry using
the ‘Get a Single Entry’ API request, you would get all the details of the entry in the response, but the value against the Categories field
would be UID of the referenced entry (i.e., UID of the ‘Mobiles’ entry in this case).

//In order to fetch the details of the entry used in the Categories reference field, you need to
//use the include[] parameter in the following manner:

use Contentstack\Contentstack;
$stack = Contentstack::Stack("API_KEY", "DELIVERY_TOKEN", "ENVIRONMENT");
$results = $stack->ContentType('product')->Query()->toJSON()->includeReference(array('categories'))->find();

deprecated since verion 2.2.0

To search the given string in the entries

Parameters

$search
  • string to be search in entries

Return Value

Query

Examples

In the Product content type, you have a entry text 'contentstack' in your content type, and you want to retrieve all the entries within this content type that have
values for this field anywhere with 'contentstack'.

use Contentstack\Contentstack;
$stack = Contentstack::Stack("API_KEY", "DELIVERY_TOKEN", "ENVIRONMENT");
$result = $stack->ContentType('product')->Query()->toJSON()->search('contentstack')->find();

in BaseQuery at line 227
Query regex()

To perform the regular expression test on the specified field

Return Value

Query

Examples

In the Product content type, you have a field named Color ("uid":"color") in your content type, and you want to retrieve all the entries within this content type that have
values for this field starting with 'Bl'.

use Contentstack\Contentstack;
$stack = Contentstack::Stack("API_KEY", "DELIVERY_TOKEN", "ENVIRONMENT");
$result = $stack->ContentType('product')->Query()->toJSON()->regex('color','^B1')->find();

Now, in order to perform a case-insensitive search, you can use the $options key to specify any regular expressions options:
use Contentstack\Contentstack;
$stack = Contentstack::Stack("API_KEY", "DELIVERY_TOKEN", "ENVIRONMENT");
$result = $stack->ContentType('product')->Query()->toJSON()->regex('color','^B1','i')->find();

in BaseQuery at line 256
Query logicalAND()

Logical AND queries are pushed

Return Value

Query

Examples

Let’s say you want to retrieve entries in which the Title field is set to 'Redmi Note
3' and the Color field is 'Gold'. The query to be used for such a case would be:

use Contentstack\Contentstack;
$stack = Contentstack::Stack("API_KEY", "DELIVERY_TOKEN", "ENVIRONMENT");
$query1 = $stack->ContentType('product')->Query()->where('title', 'Redmi Note 3');
$query2 = $stack->ContentType('product')->Query()->where('color', 'Gold');
$entries = $stack->ContentType('product')->Query()->logicalAND($query1, $query2)->toJSON()->find();

in BaseQuery at line 283
Query logicalOR()

Logical OR queries are pushed

Return Value

Query

Examples

Let’s say you want to retrieve entries in which either the value for the Color field is 'Gold' or 'Black'.
The query to be used for such a case would be:

use Contentstack\Contentstack;
$stack = Contentstack::Stack("API_KEY", "DELIVERY_TOKEN", "ENVIRONMENT");
$query1 = $stack->ContentType('product')->Query()->where('color', 'Black');
$query2 = $stack->ContentType('product')->Query()->where('color', 'Gold');
$entries = $stack->ContentType('product')->Query()->logicalOR($query1, $query2)->toJSON()->find();

in BaseQuery at line 307
Query ascending($field_uid = '')

To sort the entries in ascending order of the specified field

Parameters

$field_uid
  • field uid to be sorted

Return Value

Query

Examples

In the Product content type, if you wish to sort the entries with respect to their prices in ascending order.

use Contentstack\Contentstack;
$stack = Contentstack::Stack("API_KEY", "DELIVERY_TOKEN", "ENVIRONMENT");
$result = $stack->ContentType('product')->Query()->toJSON()->ascending('price')->find();

in BaseQuery at line 331
Query descending($field_uid = '')

To sort the entries in descending order of the specified field

Parameters

$field_uid
  • field uid to be sorted

Return Value

Query

Examples

In the Product content type, if you wish to sort the entries with respect to their prices in descending order.

use Contentstack\Contentstack;
$stack = Contentstack::Stack("API_KEY", "DELIVERY_TOKEN", "ENVIRONMENT");
$result = $stack->ContentType('product')->Query()->toJSON()->descending('price')->find();

in BaseQuery at line 356
Query notExists($field_uid = '')

To check field doesn't exists

Parameters

$field_uid
  • field uid against the value not existence is checked

Return Value

Query

Examples

In the Product content type, if we need to retrieve the data of entries of all the other fields except the Price in USD parameter.

use Contentstack\Contentstack;
$stack = Contentstack::Stack("API_KEY", "DELIVERY_TOKEN", "ENVIRONMENT");
$result = $stack->ContentType('product')->Query()->toJSON()->notExists('price')->find();

in BaseQuery at line 382
Query exists($field_uid = '')

To check field exists

Parameters

$field_uid
  • field uid against the value existence is checked

Return Value

Query

Examples

In the Product content type, if we need to retrieve the data of only the Price in USD parameter of all the entries.

use Contentstack\Contentstack;
$stack = Contentstack::Stack("API_KEY", "DELIVERY_TOKEN", "ENVIRONMENT");
$result = $stack->ContentType('product')->Query()->toJSON()->exists('price')->find();

in BaseQuery at line 405
Query includeFallback()

To include fallback content if specified locale content is not publish.

Return Value

Query

Examples

use Contentstack\Contentstack;
$stack = Contentstack::Stack("API_KEY", "DELIVERY_TOKEN", "ENVIRONMENT");
$result = $stack->ContentType('product')->Query()->toJSON()->includeFallback()->find();

in BaseQuery at line 427
Query includeBranch()

To include branch of publish content.

Return Value

Query

Examples

use Contentstack\Contentstack;
$stack = Contentstack::Stack("API_KEY", "DELIVERY_TOKEN", "ENVIRONMENT");
$result = $stack->ContentType('product')->Query()->toJSON()->includeBranch()->find();

in BaseQuery at line 452
Query includeSchema() deprecated

deprecated since verion 1.1.0

To include schema along with entries

Return Value

Query

Examples

use Contentstack\Contentstack;
$stack = Contentstack::Stack("API_KEY", "DELIVERY_TOKEN", "ENVIRONMENT");
$result = $stack->ContentType('product')->Query()->toJSON()->includeSchema()->find();

in BaseQuery at line 475
Query includeReferenceContentTypeUID()

This method includes the content type UIDs of the referenced entries returned in the response.

Return Value

Query

Examples

use Contentstack\Contentstack;
$stack = Contentstack::Stack("API_KEY", "DELIVERY_TOKEN", "ENVIRONMENT");
$result = $stack->ContentType('product')->Query()->toJSON()->includeReferenceContentTypeUID()->find();

in BaseQuery at line 496
Query includeContentType()

To include content_type along with entries

Return Value

Query

Examples

use Contentstack\Contentstack;
$stack = Contentstack::Stack("API_KEY", "DELIVERY_TOKEN", "ENVIRONMENT");
$result = $stack->ContentType('product')->Query()->toJSON()->includeContentType()->find();

in BaseQuery at line 517
Query includeEmbeddedItems()

To include Embedded Items along with entries

Return Value

Query

Examples

use Contentstack\Contentstack;
$stack = Contentstack::Stack("API_KEY", "DELIVERY_TOKEN", "ENVIRONMENT");
$result = $stack->ContentType('product')->Query()->toJSON()->includeEmbeddedItems()->find();

in BaseQuery at line 539
Query includeCount()

To include the count of entries based on the results

Return Value

Query

Examples

use Contentstack\Contentstack;
$stack = Contentstack::Stack("API_KEY", "DELIVERY_TOKEN", "ENVIRONMENT");
$result = $stack->ContentType('product')->Query()->toJSON()->includeCount()->find();

in BaseQuery at line 560
Query count()

To get only count result

Return Value

Query

Examples

use Contentstack\Contentstack;
$stack = Contentstack::Stack("API_KEY", "DELIVERY_TOKEN", "ENVIRONMENT");
$result = $stack->ContentType('product')->Query()->toJSON()->count()->find();

in BaseQuery at line 582
Query includeOwner()

To include the owner of entries based on the results

Return Value

Query

Examples

use Contentstack\Contentstack;
$stack = Contentstack::Stack("API_KEY", "DELIVERY_TOKEN", "ENVIRONMENT");
$result = $stack->ContentType('product')->Query()->toJSON()->includeOwner()->find();

in BaseQuery at line 606
Query addParam(string $key = '', string $value = '')

To add query parameter in query

Parameters

string $key
  • Name of key in string
string $value
  • Value of the key in string

Return Value

Query

Examples

use Contentstack\Contentstack;
$stack = Contentstack::Stack("API_KEY", "DELIVERY_TOKEN", "ENVIRONMENT");
$result = $stack->ContentType('product')->Query()->addParam('include_count', 'true')->toJSON()->find();

in BaseQuery at line 630
Query language($lang = '')

To set the language code in the query

Parameters

$lang
  • Language code by default is "en-us"

Return Value

Query

Examples

use Contentstack\Contentstack;
$stack = Contentstack::Stack("API_KEY", "DELIVERY_TOKEN", "ENVIRONMENT");
$result = $stack->ContentType('product')->Query()->toJSON()->language('en-us')->find();

in BaseQuery at line 654
Query skip(int $skip = 0)

Skip the specified number of entries from result set

Parameters

int $skip
  • valid number

Return Value

Query

Examples

The skip parameter will skip a specific number of entries in the output. So, for example, if the content type contains around 12 entries
and you want to skip the first 2 entries to get only the last 10 in the response body, you need to specify ‘2’ here.

use Contentstack\Contentstack;
$stack = Contentstack::Stack("API_KEY", "DELIVERY_TOKEN", "ENVIRONMENT");
$result = $stack->ContentType('product')->Query()->toJSON()->skip(2)->find();

in BaseQuery at line 677
Query tags(array $tags = array())

Result set entries should have tags specified

Parameters

array $tags
  • array of tags you want to match in the entries tags

Return Value

Query

Examples

use Contentstack\Contentstack;
$stack = Contentstack::Stack("API_KEY", "DELIVERY_TOKEN", "ENVIRONMENT");
$result = $stack->ContentType('product')->Query()->toJSON()->tags(array('Vivo','Gold'))->find();

in BaseQuery at line 705
Query limit(int $limit = '')

Limit the specified number of entries from result set

Parameters

int $limit
  • valid number

Return Value

Query

Examples

The limit parameter will return a specific number of entries in the output.
So for example, if the content type contains more than 100 entries and you wish to fetch only the first 2 entries, you need to specify '2' as value in this parameter.

use Contentstack\Contentstack;
$stack = Contentstack::Stack("API_KEY", "DELIVERY_TOKEN", "ENVIRONMENT");
$result = $stack->ContentType('product')->Query()->toJSON()->limit(2)->find();

in BaseQuery at line 739
Query containedIn(string $field = '', array $value = array())

Query the field value from the given set of values

Parameters

string $field
  • field in the entry against which comparision needs to be done
array $value
  • array value against which comparision is going to happen

Return Value

Query

Examples

Example 1 - Array Equals Operator Within Group
use Contentstack\Contentstack;
$stack = Contentstack::Stack("API_KEY", "DELIVERY_TOKEN", "ENVIRONMENT");
$result = $stack->ContentType('product')->Query()->toJSON()->containedIn('title',array('Redmi','Samsung'))->find();


Example 2 - Array Equals Operator Within Modular Blocks
use Contentstack\Contentstack;
$stack = Contentstack::Stack("API_KEY", "DELIVERY_TOKEN", "ENVIRONMENT");
$result = $stack->ContentType('product')->Query()->toJSON()->containedIn("additional_info.deals.deal_name", ["Christmas Deal", "Summer Deal"])->find();

in BaseQuery at line 775
Query notContainedIn(string $field = '', array $value = array())

Query the field value other than the given set of values

Parameters

string $field
  • field in the entry against which comparision needs to be done
array $value
  • array value against which comparision is going to happen

Return Value

Query

Examples

Example 1 - Array Not-equals Operator Within Group
use Contentstack\Contentstack;
$stack = Contentstack::Stack("API_KEY", "DELIVERY_TOKEN", "ENVIRONMENT");
$result = $stack->ContentType('product')->Query()->toJSON()->notContainedIn("title", ["Electronics", "Apparel"])->find();

Example 2 - Array Not-equals Operator Within Modular Blocks
use Contentstack\Contentstack;
$stack = Contentstack::Stack("API_KEY", "DELIVERY_TOKEN", "ENVIRONMENT");
$result = $stack->ContentType('product')->Query()->toJSON()->notContainedIn("additional_info.deals.deal_name", ["Christmas Deal", "Summer Deal"]) ->find();

in BaseQuery at line 806
Query where(string $key = '', string $value = '')

Query the field which has exact value as specified

Parameters

string $key
  • field in the entry against which comparision needs to be done
string $value
  • value against which comparision is going to happen

Return Value

Query

Examples

In the Products content type, you have a field named Title ("uid":"title") field. If, for instance,
you want to retrieve all the entries in which the value for the Title field is 'Redmi 3S', you can set the parameters as:

use Contentstack\Contentstack;
$stack = Contentstack::Stack("API_KEY", "DELIVERY_TOKEN", "ENVIRONMENT");
$result = $stack->ContentType('product')->Query()->toJSON()->where('title','Redmi 3S')->find();

in BaseQuery at line 829
Query lessThan(string $field = '', string $value = '')

Query the field which has less value than specified one

Parameters

string $field
  • field in the entry against which comparision needs to be done
string $value
  • value against which comparision is going to happen

Return Value

Query

Examples

Let’s say you want to retrieve all the entries that have value of the Price in USD field set to a value that is less than but not equal to 600. You can send the parameter as:

use Contentstack\Contentstack;
$stack = Contentstack::Stack("API_KEY", "DELIVERY_TOKEN", "ENVIRONMENT");
$result = $stack->ContentType('product')->Query()->toJSON()->lessThan('price','600')->find();

in BaseQuery at line 856
Query lessThanEqualTo(string $field = '', string $value = '')

Query the field which has less or equal value than specified one

Parameters

string $field
  • field in the entry against which comparision needs to be done
string $value
  • value against which comparision is going to happen

Return Value

Query

Examples

Let’s say you want to retrieve all the entries that have value of the Price in USD field set to a value that is less than or equal to 146. To achieve this, send the parameter as:

use Contentstack\Contentstack;
$stack = Contentstack::Stack("API_KEY", "DELIVERY_TOKEN", "ENVIRONMENT");
$result = $stack->ContentType('product')->Query()->toJSON()->lessThanEqualTo('price','146')->find();

in BaseQuery at line 883
Query greaterThan(string $field = '', string $value = '')

Query the field which has greater value than specified one

Parameters

string $field
  • field in the entry against which comparision needs to be done
string $value
  • value against which comparision is going to happen

Return Value

Query

Examples

Let’s say you want to retrieve all the entries that have value of the Price in USD field set to a value that is greater than but not equal to 146. You can send the parameter as:

use Contentstack\Contentstack;
$stack = Contentstack::Stack("API_KEY", "DELIVERY_TOKEN", "ENVIRONMENT");
$result = $stack->ContentType('product')->Query()->toJSON()->greaterThan('price','146')->find();

in BaseQuery at line 910
Query greaterThanEqualTo(string $field = '', string $value = '')

Query the field which has greater or equal value than specified one

Parameters

string $field
  • field in the entry against which comparision needs to be done
string $value
  • value against which comparision is going to happen

Return Value

Query

Examples

Let’s say you want to retrieve all the entries that have value of the Price in USD field set to a value that is less than and equal to 146. You can send the parameter as:

use Contentstack\Contentstack;
$stack = Contentstack::Stack("API_KEY", "DELIVERY_TOKEN", "ENVIRONMENT");
$result = $stack->ContentType('product')->Query()->toJSON()->greaterThanEqualTo('price','146')->find();

in BaseQuery at line 937
Query notEqualTo(string $field = '', string $value = '')

Query the field which has not equal to value than specified one

Parameters

string $field
  • field in the entry against which comparision needs to be done
string $value
  • value against which comparision is going to happen

Return Value

Query

Examples

Let’s say you want to retrieve all the entries that have value of the Price in USD field set to a value that is not equal to 500. You can send the parameter as:

use Contentstack\Contentstack;
$stack = Contentstack::Stack("API_KEY", "DELIVERY_TOKEN", "ENVIRONMENT");
$result = $stack->ContentType('product')->Query()->toJSON()->notEqualTo('price','500')->find();

in BaseQuery at line 962
Query addQuery(array $_query = array())

Add Query is used to add the raw/array query to filter the entries

Parameters

array $_query
  • array formatted query

Return Value

Query

Examples

use Contentstack\Contentstack;
$stack = Contentstack::Stack("API_KEY", "DELIVERY_TOKEN", "ENVIRONMENT");
$_set = ['vivo', 'samsung', 'redmi 3', 'apple'];
$query1 = $stack->ContentType('product')->Query()->lessThan('title', $_set)->getQuery();
$_entries = $stack->ContentType('product')->Query()->addQuery($query1)->toJSON()->find();

in BaseQuery at line 982
query getQuery()

Get the raw/array query from the current instance of Query/Entry

Return Value

query

Examples

use Contentstack\Contentstack;
$stack = Contentstack::Stack("API_KEY", "DELIVERY_TOKEN", "ENVIRONMENT");
$query1 = $stack->ContentType('product')->Query()->greaterThan('price', '5000')->getQuery();

at line 55
Request find()

Get all entries based on the specified subquery

Return Value

Request

at line 73
Request findOne() deprecated

deprecated since verion 1.1.0

Get single entry based on the specified subquery

Return Value

Request