Forum
Struggling to retrieve blogSlug from post
I am new to Perch. I am building a site which has two separate blogs (knowledge and perspective). The posts from these blogs utilise the same template, post.php and the design includes a next and previous navigation to loop through posts of the same blog.
I am therefore facing the challenge of retrieving the current blogSlug such that I can pass it into the perch_blog_custom function when retrieving the next and previous links.
My code is as below:
<?php
$post = perch_blog_custom(array(
'filter'=>'postSlug',
'match'=>'eq',
'value'=>perch_get('s'),
'skip-template'=>true,
));
//echo var_dump($post);
$date = $post[0]['postDateTime'];
$blog = perch_blogs(array(
'filter'=>'blogID',
'match'=>'eq',
'value'=>'6',
'skip-template'=>true
));
//echo $blog[0]['blogSlug'];
$prev = perch_blog_custom(array(
'count'=>1,
'filter'=>'postDateTime',
'match'=>'lt',
'sort'=>'postDateTime',
'sort-order'=>'DESC',
'blog' => 'knowledge',
'value'=>$date,
'template'=>'blog/post_prev.html'
), true);
$next = perch_blog_custom(array(
'count'=>1,
'filter'=>'postDateTime',
'match'=>'gt',
'sort'=>'postDateTime',
'sort-order'=>'DESC',
'blog'=> 'knowledge',
'value'=>$date,
'template'=>'blog/post_next.html'
), true);
?>
The problems I have appear to be the following:
- My ability to var_dump the output of queries seem to execute once and then due to caching potentially just return "String" or "Array" or even just "A" and not the object contents itself meaning that debugging this issue is very difficult, even after discovering define('PERCH_DEBUG', true);. How do people inspect returned objects like this in Perch?
- I'd have expected to be able to take the blogSlug either from the current context or the current post but neither seem to hold this information. Am I right in trying to make another query to the actual blog of the post?
- I can't seem to return the slug from the perch_blogs function, as expected $blog[0]['blogSlug'] does not return the string "knowledge". Am I missing something in the code here?
Kind regards,
Ben
Perch Runway: 3.1.2, PHP: 7.2.8, MySQL: mysqlnd 5.0.12-dev - 20150407 - $Id: 38fea24f2847fa7519001be390c98ae0acafe387 $, with PDO Server OS: Darwin, apache2handler Installed apps: content (3.1.2), assets (3.1.2), categories (3.1.2), perch_blog (5.6.1), perch_events (1.9.5) App runtimes: <?php $apps_list = [ 'perch_blog', 'perch_events', ]; PERCH_LOGINPATH: /perch PERCH_PATH: /Users/bmozza/Projects/quaich/perch PERCH_CORE: /Users/bmozza/Projects/quaich/perch/core PERCH_RESFILEPATH: /Users/bmozza/Projects/quaich/perch/resources Image manipulation: GD PHP limits: Max upload 32M, Max POST 8M, Memory: 128M, Total max file upload: 8M F1: 3b606135b33e6a102526838f4152a807 Resource folder writeable: Yes HTTP_HOST: me.local:5757 DOCUMENT_ROOT: /Users/bmozza/Projects/quaich REQUEST_URI: /perch/core/settings/diagnostics/ SCRIPT_NAME: /perch/core/settings/diagnostics/index.php
I'm aware that the perch_blogs function is intended to take $post[0]['blogID'] in my code, I've just hardcoded to '6' at the moment to ensure that the right entry is being returned.
perch_blog_custom()
doesn't cache, so it should return the same thing each time. Can you give an example where it doesn't?I think it's the caching of perch_blogs function that is causing the issue with inspecting via var_dump. Are you able to speak to the 3 points that I've raised above?
You can turn off the caching with
What happens when you do so?
Hi,
The
blogID
may be enough in your case. Instead of using theblog
option, I think you can filter byblogID
:Hussein,
Your solution looks to exactly what I needed and if I filter by just blogID there is a return of the posts within that blog as expected. However, when running the multiple filters, I'm getting an OR statement in the query which breaks what I need from it. Hence, I wonder whether I do still need to use the 'blog' key to create the AND filter.
Drew,
Interestingly, turning off caching for me is what was needed. This now means that I get back the blog, can take the slug and then pass that to perch_blog_custom on each request.
So, it works. But it is ugly as hell and I feel like I'm working against the grain of Perch rather than with it. Should it feel this difficult? Am I just trying to do something that is too bold for a CMS like Perch? Should I just stick to what comes out of the box?
I believe you need to use
'filter-mode' => 'ungrouped'
:Hussein,
That's the badger. Any idea why this isn't in documentation?
Ben
As far as I'm aware this is only required for add-ons like Blog and Shop. So you don't need to use it if you're filtering a Collection for example.
I agree; it would be helpful to include this in the documentation.