Forum

Thread tagged as: Question

Filter blog posts by matching categories against another

Hi there,

I'm struggling with a particular issue which I was hoping someone could help me with. On my blog there is a category of articles called Press Coverage On each blog post I am looking to output these articles, which is what i've done

perch_blog_custom(array(
    'filter' => 'postSlug',
    'match' => 'neq',
    'value' => perch_get('s'),
    'category' => REGION.'/coverage',
    'count'=> 5,
    'blog'     => REGION,
    'sort'       => 'postDateTime',
    'sort-order' => 'DESC',
    'template'=> 'blog/post_coverage.html'
));

The part i'm struggling with is I want to filter these press coverage article to only show ones that are related to a different category.

So I have created a separate category set called Segments with the following options

  • Foodservice
  • Building Care
  • Industry
  • Health Care
  • Automotive
  • Aerospace

On each blog post, the client has to select which segment or segments it belongs to.

So If they add a press article and selects Building Care as the segment I want this press article to appear on every post that has a category of Building Care.

But I am struggling to figure out how to do this?

I've done something like this but i'm not sure if im on the right path

$segment = perch_blog_custom(array(
    'filter' => 'postSlug',
    'match' => 'eq',
    'value' => perch_get('s'),
    'template'=> 'blog/segment.html'
), true);

perch_blog_custom(array(
    'filter' => 'postSlug',
    'match' => 'neq',
    'value' => perch_get('s'),
    'category' => REGION.'/coverage', "$string",),
    'count'=> 5,
    'blog'     => REGION,
    'sort'       => 'postDateTime',
    'sort-order' => 'DESC',
    'template'=> 'blog/post_coverage.html'
));

// HTML TEMPLATE for segment.html is

<perch:categories id="segments" set="segments" label="Segments" display-as="checkboxes">
    segments/<perch:category id="catSlug" type="slug" />
</perch:categories>

Can someone please help me with this

Thanks Barry

Fishtank Creative

Fishtank Creative 2 points

  • 4 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

You'll need to use skip-template and the return-html option so you can get both the data and the markup.

echo the markup as usual.

Then fish out the related category from the data and requery for the posts in the related category.

Hi Drew,

Thanks for that, my php skills aren't the best but i've had a go with the following

$segment = perch_blog_custom(array(
                            'filter' => 'postSlug',
                            'match' => 'eq',
                            'value' => perch_get('s'),
                            'return-html' => true,
                            'template'=> 'blog/segment.html'
                        ), true);

                        // echo $segment;

                        $post2 = perch_blog_custom(array(
                            'category' => REGION.'/coverage',
                            'skip-template' => 'true',
                            'return-html'   => 'true',
                            'template'=> 'blog/post_coverage.html'
                        ));

                        echo $post2['html'];


                        // perch_blog_custom(array(
                        //     'filter' => 'postSlug',
                        //     'match' => 'neq',
                        //     'value' => perch_get('s'),
                        //     'category' => array(REGION.'/coverage', "$segment"),
                        //     'category-match' => all,
                        //     'count'=> 1,
                        //     'blog'     => REGION,
                        //     'sort'       => 'postDateTime',
                        //     'sort-order' => 'DESC',
                        //     'template'=> 'blog/post_coverage.html'
                        // ));

I'm just not sure how to requery the posts in the related category.

The above code outputs the articles i'm just not sure how to link the two up and requery.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Do you have the category data?

I am now able to output the category selected and the coverage articles

The last bit is requering them, i'm not sure about that part.

$cats = perch_categories([
                            'set' => 'segments',
                            'skip-template' => 'true',
                            'return-html'   => 'true'
                        ]);

                        $post2 = perch_blog_custom(array(
                            'category' => array(REGION.'/coverage', "$cats"),
                            'skip-template' => 'true',
                            'return-html'   => 'true',
                            'template'=> 'blog/post_coverage.html'
                        ));

                        echo($cats[0]['catSlug']);
                        echo $post2['html'];

Thanks for your help so far

Well this echo($cats[0]['catSlug']); only outputs the first category in the set called segments

Ok, i've got this working to a point

$press = perch_blog_custom(array(
                        'filter' => 'postSlug',
                        'match' => 'eq',
                        'value' => perch_get('s'),
                        'skip-template' => 'true',
                        'return-html' => 'true',
                    ));

                    print_r(array(
                        'template'=> 'blog/post_coverage.html',
                        'category' => $post[0]['segments'],
                        'category-match' => 'any',
                        'count' => 3,
                        // Filter Out Current Case Study
                        'filter' => 'postSlug',
                        'match' => 'neq',
                        'value' => perch_get('s'),
                    ));

This outputs articles that has the same segment category, I just need to add in the coverage category thats in a separate set. Not sure how to do that.

I tried adding this

'category' => array(REGION.'/coverage', $post[0]['segments']),

But its not having it, comes out like

Array ( [template] => blog/post_coverage.html [category] => Array ( [0] => en-eu/coverage [1] => Array ( [0] => segments/building-care/ ) ) [category-match] => any [count] => 3 [filter] => postSlug [match] => neq [value] => 2016-11-10-new-chicopee-range-makes-light-work-of-floor-cleaning )

I'm essentially trying to output different categories from different sets

Ok i've got this working to a point like so,

$press = perch_blog_custom(array(
                            'filter' => 'postSlug',
                            'match' => 'eq',
                            'value' => perch_get('s'),
                            'skip-template' => 'true',
                            'return-html' => 'true',
                        ));

                        $coverage = array(REGION.'/coverage');

                        perch_blog_custom(array(
                            'template'=> 'blog/post_coverage.html',
                            'category' => array_merge($press[0]['segments'], $coverage),
                            'category-match' => 'all',
                            'count' => 3,
                            'blog'     => REGION,
                            'filter' => 'postSlug',
                            'match' => 'neq',
                            'value' => perch_get('s'),
                        ));

The problem i'm having now is on some articles, the segments category set hasn't been used, so when that happens we get an error saying:

Warning: array_merge(): Argument #1 is not an array in /Users/Barry/Sites/chicopeesolutions/admin/templates/pages/blog/post.php on line 81
Call Stack
#   Time    Memory  Function    Location
1   0.0012  376344  {main}( )   .../start.php:0
2   0.1404  3136632 include( '/Users/Barry/Sites/chicopeesolutions/admin/templates/pages/blog/post.php' )   .../start.php:74
3   0.7630  4090368 array_merge ( ) .../post.php:81

Is there a way to remove the related press coverage section if the segment set hasn't been used?

Thanks

I wrapped it in an if statement

if(!empty($press[0]['segments'])) {
                            perch_blog_custom(array(
                                'template'=> 'blog/post_coverage.html',
                                'category' => array_merge($press[0]['segments'], $coverage),
                                'category-match' => 'all',
                                'count' => 3,
                                'blog'     => REGION,
                                'filter' => 'postSlug',
                                'match' => 'neq',
                                'value' => perch_get('s'),
                            ));
                        }

Seems to work ok, Is this what you were thinking from your first reply? Or have I done it completely different?

Thanks for the help

Drew McLellan

Drew McLellan 2638 points
Perch Support

If it's working, then I think you're probably good.