<p>You can access 'post' or 'get' parameters using <code>craft.request.getParam('param')</code>; and then use them to retrieve the corresponding entry. So, given the URL:</p>

<pre><code>http://example.com/subsection/filename.html?id="170"
</code></pre>

<p>In your template, you would grab the param 'id', and use it to retrieve the entry.</p>

<pre><code>{% set entryId = craft.request.getParam('id') %}
{% set entry = craft.entries.id(entryId).first() %}
{% if entry %}
    {% set pageTitle = entry.title %}
{% endif %}
</code></pre>

<p>Notice the use of <code>.first()</code>. Results from <code>craft.entries</code> are returned as an ElementCriteriaModel, which is an 'array-like' object, even if only one 'entry' is defined — <code>.first()</code> returns the first element. To avoid errors if no entries are found matching the criteria, check that 'entry' exists before trying to access its properties.</p>

<p>Likewise, do not try and loop through an entry retrieved using <code>.first()</code>, which will also throw an error. </p>

CC BY-SA 3.0
2015-02-17T23:06:22.713Z
2015-02-17T23:06:22.713Z
Douglas McDonald
<p>This is a longer version answer (code wise) to the one Douglas already posted. He did a very nice job of explaining how to get a URL parameter and what to pay attention to handling the results.</p>

<p>I will try to show you how to build a query from multiple unknown params, so any of these combinations is possible and potentially return a single (and unique) Entry Model:</p>

<p><a href="http://example.com/foo?sectionId=6&amp;entryId=170&amp;slug=somethingclever">http://example.com/foo?sectionId=6&amp;entryId=170&amp;slug=somethingclever</a><br>
<a href="http://example.com/foo?sectionId=6&amp;slug=somethingclever">http://example.com/foo?sectionId=6&amp;slug=somethingclever</a><br>
<a href="http://example.com/foo?entryId=170">http://example.com/foo?entryId=170</a>  </p>

<p>This also tries to handle the case, the page already comes with an <code>entry</code> variable pre-populated or there's already a <code>pageTitle</code> set. So you don't do all the requests / logic on all of your pages.</p>

<pre><code>{#
 # Only try parsing the params, if this page doesn't come with an `entry`
 # var pre-populated and we don't already have `pageTitle` set.
 #}
{% if not entry and pageTitle is not defined %}

    {# Get URL params #}
    {% set entryId = craft.request.getParam('entryId') %}
    {% set entrySlug = craft.request.getParam('slug') %}
    {% set sectionId = craft.request.getParam('sectionId') %}

    {# Default query #}
    {% set query = craft.entries %}

    {# Add params to the query if they are not empty #}
    {% set query = not entryId ? query : query.id(entryId) %}
    {% set query = not entrySlug ? query : query.slug(entrySlug) %}
    {% set query = not sectionId ? query : query.sectionId(sectionId) %}

    {# Now try to get that entry and set our own `entry` var #}
    {% set entry = query.first() %}

{% endif %}

{# Set the entry title. Also handle the case our query didn't find anything #}
{% set entryTitle = entry ? entry.title : 'Title for a page that doesn’t exist' %}

{# Set the page's title #}
{% set activeTitle = pageTitle is defined ? pageTitle : entryTitle %}
</code></pre>

<p>Useful links for this code: <a href="http://buildwithcraft.com/docs/templating/craft.request"><code>craft.request</code></a> and <a href="http://twig.sensiolabs.org/doc/templates.html#other-operators">Ternary operator in Twig</a>.</p>

CC BY-SA 3.0
2015-02-18T08:28:09.237Z
2015-02-18T08:42:29.847Z
2015-02-18T08:42:29.847Z
carlcs
<p>I have a bit of code that checks if a page's title has been manually set in a parent template, and if it has to use that, otherwise to use <code>entry.title</code>. This is what that bit looks like:</p>

<pre><code>{% set activeTitle %}
     {% if pageTitle is defined %}
         {{ pageTitle }}
     {% else %}
         {{ entry.title }}
     {% endif %}
{% endset %}
</code></pre>

<p>I however have some pages like so:<br>
<a href="http://example.com/foo/text.html?sectionId=6&amp;handle=bar&amp;entryId=170&amp;slug=somethingclever" rel="noreferrer">http://example.com/foo/text.html?sectionId=6&amp;handle=bar&amp;entryId=170&amp;slug=somethingclever</a></p>

<p>How do I get the entry based on the parameters that are defined in the URL? And then use this entry's <code>entry.title</code> to set my page title?</p>

CC BY-SA 3.0
2015-02-16T23:38:04.737Z
What do you mean with "'set' 'entry'" and which URL parameter should be relevant? All? I am confused, sorry. And what does this have to do with "entry title" or the code setting `pageTitle`?
carlcs
CC BY-SA 3.0
2015-02-17T00:05:11.167Z
Hey calcs it actually doesn't have much to do with that. I'm running on fumes here. I have a variable "activeTitle" that gets set based on whether there is a manual title 'pageTitle' defined on the parent template or not. If there is not one, it uses the 'entry.title'. I have special case where a query is output 'such as shown in that link' and i need to define what 'entry' that particular query is calling so that the variable 'activeTitle' can be defined. As of now it's a null variable. I basically need to let the page know, what entry should be being called based on the query string in url.
Christopher Healey
CC BY-SA 3.0
2015-02-17T00:07:04.513Z
I've been thinking about it a bit more 'still running on fumes' is there an implied for loop on pages that the system knows there is an entry defined for? If that is the case for pages like my special case where the system does not know automatically what entry it should be showing, do i need to create an additional conditional for loop?
Christopher Healey
CC BY-SA 3.0
2015-02-17T00:35:20.307Z
I'm confused, too... are you just wanted to manually grab an entry by its Id?  `set entry = craft.entries.id(170).first()`
Brad Bell
CC BY-SA 3.0
2015-02-17T00:42:45.147Z
Brad. Essentially yes, that's a part of it. I moved my code a bit to test what the write code is and wrote this: {% set testvariable = craft.request.getParam('entryId') %}
<h1>{{ testvariable }} - {% for entry in craft.entries.id(testvariable).first() %} {{ entry.title }} {% endfor %}</h1> and i get an error Impossible to access an attribute ("title") on a string variable ("170") if i put the 'testvariable' in quotes then it doesn't throw an error but no title is output. If i know how to grab an entry by a parameter in the url query i think i could sort the rest of this out but im stumped now.
Christopher Healey
CC BY-SA 3.0
2015-02-17T01:26:27.330Z
Are you sure you have an entry with an Id of 170?
Brad Bell
CC BY-SA 3.0
2015-02-17T10:56:15.413Z
Yes I am sure there is an entry with the id of 170.
Christopher Healey
CC BY-SA 3.0
2015-02-17T11:01:04.323Z
What i've done is wrapped the entire thing in a for loop , changed 'entry' to 'entryonpage' since that is what i use in my for loop and set this at the top of the for loop:       {% set theID = craft.request.getParam('entryId') %}
{% for entryonpage in craft.entries.id(theID) %} this all seems to work. Let me know if i'm doing something incorrectly it seems pretty convoluted, but that could just be because i've been staring at it and tweaking for hours haha.
Christopher Healey
<templating><entry><entrymodel><params>
Determine Entry based on URL Parameters?
# Determine Entry based on URL Parameters?

(Asked by: username_0 on 2015-02-16)

I have a bit of code that checks if a page's title has been manually set in a parent template, and if it has to use that, otherwise to use entry.title. This is what that bit looks like:
{% set activeTitle %}
     {% if pageTitle is defined %}
         {{ pageTitle }}
     {% else %}
         {{ entry.title }}
     {% endif %}
{% endset %}

I however have some pages like so:
http://example.com/foo/text.html?sectionId=6&handle=bar&entryId=170&slug=somethingclever
How do I get the entry based on the parameters that are defined in the URL? And then use this entry's entry.title to set my page title?

**Tags:** templating, entry, entrymodel, params

> **Comments:**
> **username_1**: What do you mean with "'set' 'entry'" and which URL parameter should be relevant? All? I am confused, sorry. And what does this have to do with "entry title" or the code setting `pageTitle`?  
> **username_0**: Hey calcs it actually doesn't have much to do with that. I'm running on fumes here. I have a variable "activeTitle" that gets set based on whether there is a manual title 'pageTitle' defined on the parent template or not. If there is not one, it uses the 'entry.title'. I have special case where a query is output 'such as shown in that link' and i need to define what 'entry' that particular query is calling so that the variable 'activeTitle' can be defined. As of now it's a null variable. I basically need to let the page know, what entry should be being called based on the query string in url.  
> **username_0**: I've been thinking about it a bit more 'still running on fumes' is there an implied for loop on pages that the system knows there is an entry defined for? If that is the case for pages like my special case where the system does not know automatically what entry it should be showing, do i need to create an additional conditional for loop?  
> **username_2**: I'm confused, too... are you just wanted to manually grab an entry by its Id?  `set entry = craft.entries.id(170).first()`  
> **username_0**: Brad. Essentially yes, that's a part of it. I moved my code a bit to test what the write code is and wrote this: {% set testvariable = craft.request.getParam('entryId') %} <h1>{{ testvariable }} - {% for entry in craft.entries.id(testvariable).first() %} {{ entry.title }} {% endfor %}</h1> and i get an error Impossible to access an attribute ("title") on a string variable ("170") if i put the 'testvariable' in quotes then it doesn't throw an error but no title is output. If i know how to grab an entry by a parameter in the url query i think i could sort the rest of this out but im stumped now.  
> **username_2**: Are you sure you have an entry with an Id of 170?  
> **username_0**: Yes I am sure there is an entry with the id of 170.  
> **username_0**: What i've done is wrapped the entire thing in a for loop , changed 'entry' to 'entryonpage' since that is what i use in my for loop and set this at the top of the for loop:       {% set theID = craft.request.getParam('entryId') %} {% for entryonpage in craft.entries.id(theID) %} this all seems to work. Let me know if i'm doing something incorrectly it seems pretty convoluted, but that could just be because i've been staring at it and tweaking for hours haha.  

---

## 2 answers

### Answer by username_1 on 2015-02-18. Score: 7 | Accepted answer

This is a longer version answer (code wise) to the one Douglas already posted. He did a very nice job of explaining how to get a URL parameter and what to pay attention to handling the results.
I will try to show you how to build a query from multiple unknown params, so any of these combinations is possible and potentially return a single (and unique) Entry Model:
http://example.com/foo?sectionId=6&entryId=170&slug=somethingclever
http://example.com/foo?sectionId=6&slug=somethingclever
http://example.com/foo?entryId=170 
This also tries to handle the case, the page already comes with an entry variable pre-populated or there's already a pageTitle set. So you don't do all the requests / logic on all of your pages.
{#
 # Only try parsing the params, if this page doesn't come with an `entry`
 # var pre-populated and we don't already have `pageTitle` set.
 #}
{% if not entry and pageTitle is not defined %}

    {# Get URL params #}
    {% set entryId = craft.request.getParam('entryId') %}
    {% set entrySlug = craft.request.getParam('slug') %}
    {% set sectionId = craft.request.getParam('sectionId') %}

    {# Default query #}
    {% set query = craft.entries %}

    {# Add params to the query if they are not empty #}
    {% set query = not entryId ? query : query.id(entryId) %}
    {% set query = not entrySlug ? query : query.slug(entrySlug) %}
    {% set query = not sectionId ? query : query.sectionId(sectionId) %}

    {# Now try to get that entry and set our own `entry` var #}
    {% set entry = query.first() %}

{% endif %}

{# Set the entry title. Also handle the case our query didn't find anything #}
{% set entryTitle = entry ? entry.title : 'Title for a page that doesn’t exist' %}

{# Set the page's title #}
{% set activeTitle = pageTitle is defined ? pageTitle : entryTitle %}

Useful links for this code: craft.request and Ternary operator in Twig.


---

### Answer by username_3 on 2015-02-17. Score: 5

You can access 'post' or 'get' parameters using craft.request.getParam('param'); and then use them to retrieve the corresponding entry. So, given the URL:
http://example.com/subsection/filename.html?id="170"

In your template, you would grab the param 'id', and use it to retrieve the entry.
{% set entryId = craft.request.getParam('id') %}
{% set entry = craft.entries.id(entryId).first() %}
{% if entry %}
    {% set pageTitle = entry.title %}
{% endif %}

Notice the use of .first(). Results from craft.entries are returned as an ElementCriteriaModel, which is an 'array-like' object, even if only one 'entry' is defined — .first() returns the first element. To avoid errors if no entries are found matching the criteria, check that 'entry' exists before trying to access its properties.
Likewise, do not try and loop through an entry retrieved using .first(), which will also throw an error.


---

