<p>I'm creating a lazy load/infinite scroll based on the tutorial but I want it to grab entries that are in categories. It doesn't seem to be doing anything with it at all though. It's returning the list of all of the entries.</p>
<p>I have tried it passing in the category object, the category id, slug, title etc. All same result</p>
<pre><code>&lt;?php

use Craft;
use craft\elements\Entry;
use League\Fractal\TransformerAbstract;
use craft\web\View;

class TwigTransformer extends TransformerAbstract
{

    public function __construct($settings)
    {
        $this-&gt;settings = $settings;
    }

    public function transform( $entry )
    {
        $oldMode = Craft::$app-&gt;view-&gt;getTemplateMode();
        Craft::$app-&gt;view-&gt;setTemplateMode(View::TEMPLATE_MODE_SITE);
        $html = Craft::$app-&gt;view-&gt;renderTemplate( $this-&gt;settings['templatePath'], [$this-&gt;settings['variableName'] =&gt; $entry]);
        Craft::$app-&gt;view-&gt;setTemplateMode($oldMode);

        return [
            'id' =&gt; $entry-&gt;id,
            'html' =&gt; $html
        ];
    }
}

return [
    'endpoints' =&gt; [
        'lazy-load' =&gt; function() {

            // validate and decode settings
            $hashed_settings = Craft::$app-&gt;request-&gt;getParam('settings');
            $settings = [];

            foreach ($hashed_settings as $key =&gt; $value) {
                $value = Craft::$app-&gt;security-&gt;validateData($value);
                if($value === false){
                    return false;
                }else{
                    $settings[$key] = $value;
                }
            }

            // criteria
            $criteria = [
                'section' =&gt; $settings['section'], 
                'limit' =&gt;  $settings['limit'],
                'relatedTo' =&gt; $settings['category'],
                'offset' =&gt;  $settings['limit'] * Craft::$app-&gt;request-&gt;getParam('offset'),
                'order' =&gt; $settings['orderBy'],
            ];
        
            return [
                'elementType' =&gt; Entry::class,
                'criteria' =&gt; $criteria,
                'paginate' =&gt; false,
                'transformer' =&gt; new TwigTransformer($settings),
            ];
        },
    ]
];
</code></pre>

CC BY-SA 4.0
2021-08-22T07:24:08.770Z
Does it work if you hard-code a category ID in place of `$settings['category']`?
Ben Croker
CC BY-SA 4.0
2021-08-22T14:45:14.620Z
@BenCroker it does not
Alyssa Tyler
CC BY-SA 4.0
2021-08-25T06:04:57.080Z
In that case something may be wrong with your setup. Does the entry query work in a regular Twig template?
Ben Croker
<craft3><plugin-elementapi>
Element API relatedTo not working
# Element API relatedTo not working

(Asked by: username_0 on 2021-08-21)

I'm creating a lazy load/infinite scroll based on the tutorial but I want it to grab entries that are in categories. It doesn't seem to be doing anything with it at all though. It's returning the list of all of the entries.
I have tried it passing in the category object, the category id, slug, title etc. All same result
settings = $settings;
    }

    public function transform( $entry )
    {
        $oldMode = Craft::$app->view->getTemplateMode();
        Craft::$app->view->setTemplateMode(View::TEMPLATE_MODE_SITE);
        $html = Craft::$app->view->renderTemplate( $this->settings['templatePath'], [$this->settings['variableName'] => $entry]);
        Craft::$app->view->setTemplateMode($oldMode);

        return [
            'id' => $entry->id,
            'html' => $html
        ];
    }
}

return [
    'endpoints' => [
        'lazy-load' => function() {

            // validate and decode settings
            $hashed_settings = Craft::$app->request->getParam('settings');
            $settings = [];

            foreach ($hashed_settings as $key => $value) {
                $value = Craft::$app->security->validateData($value);
                if($value === false){
                    return false;
                }else{
                    $settings[$key] = $value;
                }
            }

            // criteria
            $criteria = [
                'section' => $settings['section'], 
                'limit' =>  $settings['limit'],
                'relatedTo' => $settings['category'],
                'offset' =>  $settings['limit'] * Craft::$app->request->getParam('offset'),
                'order' => $settings['orderBy'],
            ];
        
            return [
                'elementType' => Entry::class,
                'criteria' => $criteria,
                'paginate' => false,
                'transformer' => new TwigTransformer($settings),
            ];
        },
    ]
];

**Tags:** craft3, plugin-elementapi
> **Comments:**
> **username_1**: Does it work if you hard-code a category ID in place of `$settings['category']`?  
> **username_0**: @BenCroker it does not  
> **username_1**: In that case something may be wrong with your setup. Does the entry query work in a regular Twig template?  

---

## 0 answers

