Core/find

From PunchCMS
Jump to: navigation, search

PCMS_Client->find(query, [filters], [exact], [allowedTypes])

Overview

Search through the content of the CMS using filters and types.

Arguments

query [string]
The keywords used in the search. Can be one or more words, separated by spaces.

filters [array, optional]
Filters are used to indicate one or more starting points for the search.
Possible filters are:

  • Element with API name "Shops": "element.Shops"
  • Nested Elements with template "Banner": "template.HomePage > element.Misc > templates.Banner"
  • Element with id 2399: "element.2399"

You can combine multiple starting points as items in an array.

exact [boolean, optional]
Indicate if you want to search for the exact words in the query. Default is FALSE.

allowedTypes [array, optional]
Array with allowed return elements. This way a search result can be filtered to return only Elements based on a specific template or with a specific API name. Syntax is the same as for filters.

Returns

If Elements are found matching the criteria this method returns a valid SearchResults object. Otherwise an empty collection.
That way you can test if you get a valid return using the count() method.

Example

$objCms = PCMS_Client::getInstance();
 
/* Find all Elements that have the words "cool" or "example" in 
 * there text fields.
*/
$strQuery = "cool example";
 
// Start the search on News items and Newsletters.
$arrFilters = array("element.Pages > templates.NewsItems", 
                "element.Pages > templates.NewsLetters");
 
// Return only Elements with template "News" and "Newsletter".
$arrAllowed = array("template.News", "template.NewsLetter");
 
$objResults = $objCms->find($strQuery, $arrFilters, FALSE, $arrAllowed);
foreach ($objResults as $objResult) {
    // Loop through the results.
    $objElement = $objCms->getElementById($objResult->id);
}