I want to get the custom admin grid collection in template file. How can I get that?


I have Used these link to get custom admin grid


I am Using these code in template file to get the collection

File Path:app/code/Mageplaza/HelloWorld/view/frontend/templates/helloworld_index_index.phtml

  $objectManager = $bootstrap->getObjectManager();

    //$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

    /** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $productCollection */
    $productCollection = $objectManager->create('Mageplaza\HelloWorld\Model\ResourceModel\Post\collection');
    /** Apply filters here */

     $collection = $productCollection->create()

    echo count($collection);

Can Anyone help me?

Thanks In advance.

closed as too broad by Keyur Shah, Siarhey Uchukhlebau, Keyul Shah, Rakesh Jesadiya, Qaisar Satti May 25 '17 at 8:56

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • Which grid collection you want? – Amit Bera May 22 '17 at 12:20
  • With my custom module i have created admin grid show in the screen shot prntscr.com/faq36h – Learing_Coder May 22 '17 at 12:30
  • Can you please add code for your current module? – Jaimin Sutariya May 23 '17 at 6:00
  • In need to get the admin grid collection in template file @jaimin Sutariya – Learing_Coder May 23 '17 at 6:02
  • It will require to create blocks file and get your module collection factory in it. If you can add code, it will be helpful to get correct solution. – Jaimin Sutariya May 23 '17 at 6:04

I'll suggest not to use ObjectManager.

You need to create block, template and layout files for this. Create a new file app\code\Mageplaza\HelloWorld\Block\Post.php

namespace Mageplaza\HelloWorld\Block;
use Magento\Framework\View\Element\Template;
class Post extends Template
    protected $mymodulemodelFactory;
    public function __construct(
        Template\Context $context,
        \Mageplaza\HelloWorld\Model\ResourceModel\Post\CollectionFactory $mymodulemodelFactory,
        array $data = []
    ) {

        parent::__construct($context, $data);
        $this->mymodulemodelFactory = $mymodulemodelFactory;
        $this->_isScopePrivate = true;

    public function getPosts(){
        $collection = $this->mymodulemodelFactory->create();
        return $collection;     

Now create layout file app\code\Mageplaza\HelloWorld\view\frontend\layout\helloworld_index_index.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
        <referenceContainer name="content">
            <block class="Mageplaza\HelloWorld\Block\Post" name="posts" template="Mageplaza_HelloWorld::post.phtml">                

Now create app\code\Mageplaza\HelloWorld\view\frontend\templates\post.phtml

<?php $collection = $this->getPosts(); ?>
<div class="posts">
    <?php foreach ($collection as $item){ ?>
                 <div class="post">   
                        // Display your data
     <?php  } ?> 

Now create controller file app\code\Mageplaza\HelloWorld\Controller\Index\Index.php

namespace  Mageplaza\HelloWorld\Controller\Index;
use Magento\Framework\View\Result\PageFactory;
use Magento\Framework\App\Action\Context;
class Index extends \Magento\Framework\App\Action\Action
    protected $resultPageFactory;
     public function __construct(
        Context $context,
        PageFactory $resultPageFactory
    ) {
        $this->resultPageFactory = $resultPageFactory;

    public function execute()
  • when code is compiled it is giving warning @Jaimin Sutariya – Learing_Coder May 23 '17 at 7:27
  • What is the warning? can you provide more details? – Jaimin Sutariya May 23 '17 at 7:32
  • [Exception] Warning: Declaration of Mageplaza\HelloWorld\Block\Post::_construct(Magento \Framework\View\Element\Template\Context $context, Mageplaza\HelloWorld\Mod el\ResourceModel\Post\CollectionFactory $mymodulemodelFactory, array $data = Array) should be compatible with Magento\Framework\View\Element\Template: :_construct() in /var/www/html/Defaultmagento216/app/code/Mageplaza/HelloWo rld/Block/Post.php on line 20 – Learing_Coder May 23 '17 at 8:18
  • There was a typo mistake in the code. Please check updated block file code. – Jaimin Sutariya May 23 '17 at 8:24
  • One more question, Can i use addAttributeTofillter in Post.php for getting collection,Thanks – Learing_Coder May 23 '17 at 8:48

Not the answer you're looking for? Browse other questions tagged or ask your own question.