Forum

Thread tagged as: Question, Blog

Filtering perch_blog_custom by multiple values

I'm creating previous/next buttons on a blog. My issue is coming in due to trying to filter by more than one id. I'm looking to get the previous post, and also make sure that it matches another value that is set ('AorB' is the id of the value). I can remove either filter and it works with just one value, but it needs both to function properly.

Here's the code with both filters on:

  $slug = perch_get('s'); // sets slug to equal postSlug
  $data = perch_blog_custom(array(
    'filter'=>'postSlug',
    'match'=>'eq',
    'value'=>$slug,
    'skip-template'=>true, 
  )); // grabs data of current post
  $data = $data[0];
  $date = $data['postDateTime']; // sets the date variable to the posts published date
  $AorB = $data['AorB']; // Determine if this is an article or blog post

  // previous button
  $prev = perch_blog_custom([
    'filter'=>[
      [
        'filter'=>'postDateTime',
        'match'=>'lt',
        'value'=>$date,
      ],
      [
        'filter'=>'AorB',
        'match'=>'eq',
        'value'=>$AorB,
      ],
    ],
    'count'=>1,
    'sort'=>'postDateTime',
    'sort-order'=>'DESC',
    'template'=>'blog/post_prev.html'
  ], true); // stores post in a variable to use later

Working code with only one filter: (gets previous post, regardless of 'AorB' value)

  // previous button
  $prev = perch_blog_custom([
    'filter'=>[
      [
        'filter'=>'postDateTime',
        'match'=>'lt',
        'value'=>$date,
      ],
    ],
    'count'=>1,
    'sort'=>'postDateTime',
    'sort-order'=>'DESC',
    'template'=>'blog/post_prev.html'
  ], true);

Outputting the code:

                <?php
                  if(empty($prev)) {
                    echo "";
                  } else {
                    echo $prev;
                  }
                ?>

Is this a bug or am I just not looking at this wrong?

Jared Dutra

Jared Dutra 0 points

  • 3 years ago
Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

Hello Jared,

Try adding 'filter-mode' => 'ungrouped' to your options.

$prev = perch_blog_custom([
'filter-mode' => 'ungrouped',
'filter'=>[
[
'filter'=>'postDateTime',
'match'=>'lt',
'value'=>$date,
],
[
'filter'=>'AorB',
'match'=>'eq',
'value'=>$AorB,
],
],
'count'=>1,
'sort'=>'postDateTime',
'sort-order'=>'DESC',
'template'=>'blog/post_prev.html'
], true); 

Hussein, That did it, thank you!