We are transitioning over to a new forum platform. Please post new issues there. Existing threads will be dealt with here unless you choose to move them over. Visit the new forum

Forum

Thread tagged as: Problem

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:

  1. 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?
  2. 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?
  3. 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

Studio Daughter

Studio Daughter 0 points

  • 10 months ago

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.

Drew McLellan

Drew McLellan 2638 points
Perch Support

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?

Drew McLellan

Drew McLellan 2638 points
Perch Support

You can turn off the caching with

'cache' => false

What happens when you do so?

Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

Hi,

The blogID may be enough in your case. Instead of using the blog option, I think you can filter by blogID:

$blogID = $post[0]['blogID'];
$date = $post[0]['postDateTime'];

$next = perch_blog_custom(array(
    'count' => 1,
    'filter' => [
        [
            'filter' => 'postDateTime',
            'match' => 'gt',
            'value' => $date,
        ],
        [
            'filter' => 'blogID',
            'value' => $blogID
        ]
    ],
    'sort' => 'postDateTime',
    'sort-order' => 'DESC',
    'template' => 'blog/post_next.html'
), true);

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?

Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

I'm getting an OR statement in the query

I believe you need to use 'filter-mode' => 'ungrouped':

$next = perch_blog_custom(array(
    'count' => 1,
    'filter' => [
        [
            'filter' => 'postDateTime',
            'match' => 'gt',
            'value' => $date,
        ],
        [
            'filter' => 'blogID',
            'value' => $blogID
        ]
    ],
    'filter-mode' => 'ungrouped',
    'sort' => 'postDateTime',
    'sort-order' => 'DESC',
    'template' => 'blog/post_next.html'
), true);

Hussein,

That's the badger. Any idea why this isn't in documentation?

Ben

Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

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.