# SoberWP Controller: Access $post objects data from archive page

**URL:** https://discourse.roots.io/t/soberwp-controller-access-post-objects-data-from-archive-page/9478
**Category:** sage
**Tags:** sage9, plugin
**Created:** 2017-04-27T19:09:28Z
**Posts:** 7

## Post 1 by @anon21078981 — 2017-04-27T19:09:29Z

I’m using the Controller plugin to grab ACF data from posts. For example:

```
public function my_value() {
    return get_field('my_value');
}
```

I need to access this data from `single` pages but also from archive pages like `front-page`, so I placed these methods in the `App` controller.

Calling `{{ $my_value }}` from `single` returns the value but returns `false` from within The Loop in `front-page` . Digging a bit deeper, I found out that the same if true when calling `get_the_ID()` from the class: the ID is retuned in single, but not for archives.

Perhaps I need to explicitly invoque the `$post` element from within the class to make it accessible, however I’m not sure how to do this.

One thing is clear, I would like to avoid having to pass the ID as a variable in template files, as is the case for single templates.

---

## Post 2 by @withjacoby — 2017-04-28T08:59:26Z

Hey @anon21078981,

So I’ve set up a use-case using your example, using a YouTube ID (as per our private discussion).

In you’re in the loop, it’s better to use a **static public function** (they do not get converted to a variable, and can be run from within your Blade template), which allows the function to be run from within the loop.

So, if you’re in an archive page, this will not work;

```
public function getYouTubeID()
{
    return get_field('youtube_id');
}
```

It’s not being run from within the loop, so it will create a single variable $get\_you\_tube with only the latest post value.

This works well in the case of a single post, but not for the loop.

The solution and correct usage would be to create a public static function, which can be run for each loop iteration.

```
public static function getYouTubeID()
{
    return get_field('youtube_id');
}
```

You would then call from within the loop (assuming your class name is Archive);

```
@while (have_posts()) @php(the_post())
    {{ Archive::getYouTubeID() }}
@endwhile
```

If you want to use the function for both archive/index and singular pages, you could turn it into a trait and include in both view controller Classes.

**Full example;**

**resources/controllers/partials/YouTubeID.php**

```
<?php

namespace App;

trait YouTubeID
{
    public static function getYouTubeID()
    {
        return get_field('youtube_id');
    }
}
```

**resources/controllers/Archive.php**

```
<?php

namespace App;

use Sober\Controller\Controller;

class Archive extends Controller
{
    use YouTubeID;
}
```

**resources/controllers/Single.php**

```
<?php

namespace App;

use Sober\Controller\Controller;

class Single extends Controller
{
    use YouTubeID;
}
```

Then, within the Blade templates;

**resources/views/archive.blade.php**

```
@extends('layouts.app')

@section('content')

  @include('partials.page-header')

  @while (have_posts()) @php(the_post())
    {{ Archive::getYouTubeID() }}
  @endwhile

  {!! get_the_posts_navigation() !!}
@endsection
```

**resources/views/single.blade.php**

```
@extends('layouts.app')

@section('content')
  @while(have_posts()) @php(the_post())
    {{ Single::getYouTubeID() }}
  @endwhile
@endsection
```

Let me know if that solves your use-case?

---

## Post 3 by @MWDelaney — 2017-04-28T11:57:31Z

Controller is so insanely useful that I think there’s an argument to be made for shipping it with Sage, or at least as a Composer package that could be optionally included with Sage and autoloaded.

I get why it’s a plugin right now, but its job is fundamental to how the theme is developed, which makes me want to package it with the theme itself.

---

## Post 4 by @anon21078981 — 2017-04-28T12:14:33Z

This could not be more clear! Thanks so much for all your help. I have everything I need now to work on the weekend :slight_smile:

---

## Post 5 by @withjacoby — 2017-04-28T12:30:57Z

No problem, and sorry it took a while! It’s been a busy week.

Let me know if you’ve got any other questions as you go!

---

## Post 6 by @MWDelaney — 2017-04-28T13:18:25Z

This is off topic, so might warrant a separate thread, but is it within your interest, @withjacoby, to abstract the plugin one step and turn the Controller functionality into a Composer package, and then include THAT in the plugin? That would let us also include said package in a Sage theme and development could occur in a single repository.

---

## Post 7 by @kaisermann — 2017-04-28T16:13:32Z

Man, that would be AWESOME. I get why it’s a plugin, but, at least in my case, controllers are so “linked” with my theme files and structure that I would much prefer to have the `soberwp/controller` as a theme dependency rather than a `mu-plugin`.
