Forum

Thread tagged as: Question

Dataselect for categories assigned to a blog post

Hi, I wonder if it's possible to have a dataselect field that gets its options from the categories that are set to a post.

I'm displaying blog posts using perch-blog-custom in index.php:

// Get matched posts
$posts = perch_blog_custom('Posts', [
        'skip-template' => true,
        'category' => $filter,
        'category-match' => $match,
        'sort' => 'postDateTime',
        'sort-order' => 'DESC',
        'data' => [
            'singlePage' => 'no',
        ],
    ]);

// Display posts
foreach ($posts as $key) {
        perch_blog_post($key['postSlug']);
}

And this is extracted from post.html:

<perch:categories id="cardFamily" set="family" label="Card family" required="true">
    <perch:category id="catSlug" />
</perch:categories>

<perch:blog id="mainFamily" label="Main family" type="dataselect" page="index.php" region="Posts" options="cardFamily">

That last line is not working, but I think it explains what I'm trying to do

Diagnostics report:

Perch: 3.1.2, PHP: 7.2.1, MySQL: mysqlnd 5.0.12-dev - 20150407 - $Id: b396954eeb2d1d9ed7902b8bae237b287f21ad9e $, 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)
App runtimes: <?php $apps_list = [ 'perch_blog', ];
PERCH_LOGINPATH: /perch
PERCH_PATH: /Users/Juan/Library/Mobile Documents/com~apple~CloudDocs/condimen/condimen projects/lib.condimen.com/perch
PERCH_CORE: /Users/Juan/Library/Mobile Documents/com~apple~CloudDocs/condimen/condimen projects/lib.condimen.com/perch/core
PERCH_RESFILEPATH: /Users/Juan/Library/Mobile Documents/com~apple~CloudDocs/condimen/condimen projects/lib.condimen.com/perch/resources
Image manipulation: GD
PHP limits: Max upload 32M, Max POST 64M, Memory: 128M, Total max file upload: 32M
F1: 3b606135b33e6a102526838f4152a807
Resource folder writeable: Yes
HTTP_HOST: condimen.local:5757
DOCUMENT_ROOT: /Users/Juan/Library/Mobile Documents/com~apple~CloudDocs/condimen/condimen projects/lib.condimen.com
REQUEST_URI: /perch/core/settings/diagnostics/
SCRIPT_NAME: /perch/core/settings/diagnostics/index.php

Thank you!

Juan Ortiz Zaforas

Juan Ortiz Zaforas 0 points

  • 2 years ago
Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

Hello Juan,

You don't manage blog posts from a content region. So region="Posts" wouldn't match anything.

Why not use output the fields using the perch:categories tags you already have?

<perch:categories id="cardFamily" set="family" label="Card family" required="true">
<!--* output any fields from the selected categories *-->
</perch:categories>

Thank you Hussein!

Now I have the following code

<!--* Set all families for the post *-->
<perch:categories id="cardFamily" set="family" label="Card family" required="true"></perch:categories>

<!--* Set main family for the post *-->
<perch:categories id="mainCardFamily" set="family" label="Main family" required="true">
    <perch:category id="catSlug" />
</perch:categories>

It works fine, but what I want is to use the categories set in cardFamily as the options for another field (mainCardFamily). This way I could set multiple categories on cardFamily and mark just one as the main category for the post on mainCardFamily. Right now I can choose from all the categories on both fields.

Any suggestions? Or it is something to do with Runway?

Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

I don't think there's a field type that does what you are looking for. Though on the mainCardFamily you can set a maximum max="1" so the editor can only select one category.

I was afraid of that. Thanks anyway for the max tip! I think I can figure out some workaround using it