Answers.Body: <p>You can't just pass a string (category name) to <code>relatedTo</code>, you need to pass either the category ID or a complete <a href="https://craftcms.com/docs/templating/categorymodel" rel="nofollow noreferrer">CategoryModel</a> (you can also pass in an array of IDs or models, if there are several categories).</p>
  
  <p>This means that unless you already have the category IDs or models, you'll need to pull them using the <a href="https://craftcms.com/docs/templating/craft.categories" rel="nofollow noreferrer"><code>craft.categories</code></a> service:</p>
  
  <pre><code>{% set management = craft.categories.title('Management').first() %}
  
  {% set staff = craft.entries.section('staffList').relatedTo(management) %}
  </code></pre>
  
  <p><em>If you have multiple category groups, and there's a possibility you might have multiple categories across those groups with the same name, you should specify which category group the category you're after belongs to:</em></p>
  
  <pre><code>{% set management = craft.categories.group('categoryGroupHandle').title('Management').first() %}
  </code></pre>
  
  <p>If you want to pull staff entries related to multiple categories, here's how that could look:</p>
  
  <pre><code>{% set categories = craft.categories.title(['Management', 'Staff', 'Other']) %}
  
  {% set staff = craft.entries.section('staffList').relatedTo(categories) %}
  </code></pre>
  
  <p>Finally, keep in mind that if you're rendering a <em>category template</em> (i.e. you've requested the URL for a particular category and Craft has rendered the template specified in the relevant category group's template path), Craft will actually populate the template with a <code>category</code> variable, containing the CategoryModel for the category belonging to that URL. If this is the case, then you simply need to do this:</p>
  
  <pre><code>{% set staff = craft.entries.section('staffList').relatedTo(category) %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-09-25T23:16:15.320Z
Answers.LastActivityDate: 2017-09-25T23:16:15.320Z
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Body: <p>How do you set a variable to a specific category so you can do an if or for loop? I have entries(staffList) associated to categories (Management,Staff,Other,PartTime)and I am trying to set a variable to do a loop off of and pull the entry fields but am having a difficult time setting the variable to pull just a specific category.</p>
  
  <p>Below is what I am using to set:</p>
  
  <pre><code>{% set staff = craft.entries.section('staffList').relatedTo('Management') %}
  </code></pre>
  
  <p>Any ideas where I am going wrong?</p>
  
Tags: <categories><craft3><set>
Title: Setting Variable to Specific Category for Loop
ThreadText: # Setting Variable to Specific Category for Loop
  
  (Asked by: username_0 on 2017-09-24)
  
  How do you set a variable to a specific category so you can do an if or for loop? I have entries(staffList) associated to categories (Management,Staff,Other,PartTime)and I am trying to set a variable to do a loop off of and pull the entry fields but am having a difficult time setting the variable to pull just a specific category.
  
  Below is what I am using to set:
  
  ```
  {% set staff = craft.entries.section('staffList').relatedTo('Management') %}
  
  ```
  
  Any ideas where I am going wrong?
  
  **Tags:** categories, craft3, set
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2017-09-25. Score: 2 | Accepted answer
  
  You can't just pass a string (category name) to `relatedTo`, you need to pass either the category ID or a complete [CategoryModel](https://craftcms.com/docs/templating/categorymodel) (you can also pass in an array of IDs or models, if there are several categories).
  
  This means that unless you already have the category IDs or models, you'll need to pull them using the [`craft.categories`](https://craftcms.com/docs/templating/craft.categories) service:
  
  ```
  {% set management = craft.categories.title('Management').first() %}
  
  {% set staff = craft.entries.section('staffList').relatedTo(management) %}
  
  ```
  
  *If you have multiple category groups, and there's a possibility you might have multiple categories across those groups with the same name, you should specify which category group the category you're after belongs to:*
  
  ```
  {% set management = craft.categories.group('categoryGroupHandle').title('Management').first() %}
  
  ```
  
  If you want to pull staff entries related to multiple categories, here's how that could look:
  
  ```
  {% set categories = craft.categories.title(['Management', 'Staff', 'Other']) %}
  
  {% set staff = craft.entries.section('staffList').relatedTo(categories) %}
  
  ```
  
  Finally, keep in mind that if you're rendering a *category template* (i.e. you've requested the URL for a particular category and Craft has rendered the template specified in the relevant category group's template path), Craft will actually populate the template with a `category` variable, containing the CategoryModel for the category belonging to that URL. If this is the case, then you simply need to do this:
  
  ```
  {% set staff = craft.entries.section('staffList').relatedTo(category) %}
  
  ```
  
  
  ---
  
  