Filtering entries based on category? I want to filter data based on category, I would prefer to do this using a join statement, but I can't figure out how an entry is related to its category. I had the same problem with matrix field, but figured it out after hours of research, the code for this is below. A similar statement to filter based on category would be great.

$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);
$criteria-&gt;section = 'affiliates';
$criteria-&gt;limit = null;
$query = craft()-&gt;elements-&gt;buildElementsQuery($criteria);
$query-&gt;join('matrixblocks matrixblocks', 'matrixblocks.ownerId = entries.id');
$mat_name = 'field_handle';
$query-&gt;join('matrixcontent_'.$mat_name.' matrixcontent_'.$mat_name, 'matrixcontent_'.$mat_name.'.elementId = matrixblocks.id');
$query-&gt;join('relations relations', 'relations.sourceId = matrixcontent_'.$mat_name.'.elementId');
$query-&gt;join('content '.$mat_name, $mat_name.'.elementId = relations.targetId');
$query-&gt;andWhere($mat_name.".title"." IN ('abc', 'def')");
Craft has a pretty solid engine for relational queries, so unless what you want to do is totally outside the box, there's a good chance you can accomplish what you want by using the relatedTo parameter available to the ElementCriteriaModel. Note that the relatedTo entry in the docs pertain to templates and not plugin development per se, but aside from the syntax the concept and approach will be identical in PHP.

Here are some examples, since its not obvious to me exactly what you hope to achieve:

To pull entries related to a certain category:

$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry, [
    'section' =&gt; 'affiliates',
    'limit' =&gt; null,
    'relatedTo' =&gt; [
        'element' =&gt; $category, // Instance of CategoryModel, or a category ID
    ],
]);


To pull entries related to multiple categories:

...
'relatedTo' =&gt; [
    'element' =&gt; $categories, // Array of CategoryModels or category IDs
],
...


To pull entries related to a category, where the relation is made in a certain Categories field:

...
'relatedTo' =&gt; [
    'element' =&gt; $category,
    'field' =&gt; 'someCategoriesFieldHandle',
],
...


To pull entries related to a category, where the relation is made in a certain Categories field on the EntryModel:

...
'relatedTo' =&gt; [
    'targetElement' =&gt; $category,
    'field' =&gt; 'someCategoriesField',
],
...


To pull entries related to a category, where the relation is made in a certain Entries field on the CategoryModel:

...
'relatedTo' =&gt; [
    'sourceElement' =&gt; $category,
    'field' =&gt; 'someEntriesField',
],
...


To pull entries with multiple possible relations, where any of the relational parameters can be met:

...
'relatedTo' =&gt; ['or', [
    'targetElement' =&gt; $categories,
    'field' =&gt; 'someCategoriesField',
], [
    'targetElement' =&gt; $someOtherCategories,
    'field' =&gt; 'someOtherCategoriesField',
]],
...


To pull entries with multiple possible relations, where all of the relational parameters must be met:

...
'relatedTo' =&gt; ['and', [
    'targetElement' =&gt; $categories,
    'field' =&gt; 'someCategoriesField',
], [
    'targetElement' =&gt; $someOtherCategories,
    'field' =&gt; 'someOtherCategoriesField',
]],
...


If you edit your question to include exactly what you want to do, I might be able to give you more specific advice or examples.