Filtering by _id
Hi,
How do I go about filtering by _id?
I have a one page detail and list view set up, and the detail view uses a different layout. What I'm trying to do is to filter the detail view to match the item that's been selected, but the code below is still outputting all the repeating regions:
<?php perch_content_custom('Properties', array(
'template'=>'property/_header_card.html',
'filter' => '_id',
'match' => 'eq',
'value' => $_GET['id'],
)); ?>
Should I be filtering the URL instead of the _id? And if so, how would I go about filtering the URL in it's current setup below?
<a href="?s=<perch:content id="slug" type="slug" />-<perch:content id="_id" type="hidden" />">
Any help would be much appreciated.
Thanks!
It looks as if your querystring contains the slug and the id concatenated together.
I would suggest removing the id and just filtering on the slug.
Thanks Rachel,
The only problem I have with that is there are lots of duplicate slugs due to the client's naming scheme for the property types. Is there a way to filter by id or a different approach I could take that would achieve the same result?
You can use multiple fields to make up the slug.
If you did just want to use the ID, just filter like this:
Is there a way to do that dynamically?
I want to show the property detail page based on the slug and ID, and I can't specify a specific ID when the client is adding properties - so I was hoping it was achievable via querying the URL with the ID as part of it.
Yes, absolutely, but you'll need to split the ID out of your slug if you're going to do that.
Brilliant, thanks Drew.
I just had a search on the docs and forums as to how to split the slug but I can't seem to find any results - do you by chance have a link to a relevant document/article that explains it?
I just found some documentation on the site, and I've now implemented the code to split the URL. But the custom_content region is not displaying anything at all - do you know where I'm going wrong?
This is my page:
And this is my custom region on a template within the detail layout:
I'm wondering if I've placed the code to split the URL in the wrong place?
You shouldn't need to filter on the slug as this point - the ID makes it unique.
What value does your
$id
have?If you turn on debug for the page, what output do you get?
Are you running the latest version of Perch and Blog?
I've now removed the filter on the slug and I wasn't on the latest version, however I've just updated to 2.6.6 and I'm getting the same result (i.e. nothing displaying).
As for the debug mode, I'm getting this where I the filter is called for the relevant template:
And this is the full debug for the page, incase you need it:
What does your
perch_content_custom()
call look like now?This is what I'm currently using:
And what does your URL look like, including the QueryString?
My URL is setup as the following:
And outputs as:
https://localhost:8888/developments/poole-court.php?s=the-roker-59
I don't understand then why you are filtering on
'value'=>perch_get('_id')
You would need a URL parameter of ?_id=
Are you still splitting off the ID at this point, if so the value needs to be your $id variable.
Ah my mistake! I've updated it to the following:
However, it's now showing all properties again rather than showing the property that matches the $id?
I'm wondering if I've set it up correctly in the page...
filter
is the name of the field to filter by.value
is the value that field should have. The two likely shouldn't be the same.I understand now. So I've changed the code to filter to "_id", but it still displays all properties rather than filters from the $id taken from the URL:
I tried changing $id to the _id number of one of the properties, and that works fine, so to me that suggests a problem with the code used to split the URL?
if you echo $id to the page, so:
<?php echo $id; ?>
What do you see?
It's not actually outputting anything on the page when I call that in the detail layout.
This is the code I have for the page itself:
If it's not outputting anything, the filter certainly won't work. You need to figure out why your ID isn't populating first.