Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ACF error on archive and 404 pages. #114

Open
christianmagill opened this issue Jan 23, 2019 · 12 comments

Comments

Projects
None yet
4 participants
@christianmagill
Copy link

commented Jan 23, 2019

I'm receiving the following warning on archive and 404 pages.


Warning: Invalid argument supplied for foreach() in /html/app/plugins/advanced-custom-fields-pro/includes/api/api-value.php on line 702

Stacktrace
----------

Fatal error: Uncaught ErrorException: Invalid argument supplied for foreach() in /html/app/plugins/advanced-custom-fields-pro/includes/api/api-value.php:702 Stack trace: 

#0 /html/app/plugins/advanced-custom-fields-pro/includes/api/api-value.php(702): App\{closure}(2, 'Invalid argumen...', '/home/...', 702, Array) 

#1 /html/app/plugins/advanced-custom-fields-pro/includes/api/api-template.php(213): acf_get_meta(0) 

#2 /html/app/plugins/advanced-custom-fields-pro/includes/api/api-template.php(169): get_field_objects(0, true) 

#3 /html/app/themes/adventhealth/vendor/soberwp/controller/src/Module/Acf.php(79): get_fields(Object(WP_Post_Type)) 

#4 /html/app/themes/adventhealth/vendor/soberwp/controller/src/Controller.php(143): Sober\Controller\Module\Acf->setData(true) 

#5 /html/app/themes/adventhealth/vendor/soberwp/controller/src/Controller.php(88): Sober\Controller\Contr in /html/app/plugins/advanced-custom-fields-pro/includes/api/api-value.php on line 702
@darrenjacoby

This comment has been minimized.

Copy link
Member

commented Jan 23, 2019

@christianmagill Hmm, just want to make sure that you're on the latest version?

@christianmagill

This comment has been minimized.

Copy link
Author

commented Jan 23, 2019

@darrenjacoby Thanks for the quick response!

Yes I am on 2.1.1, although the error also exists in 2.1.0. I updated in an effort to fix the issue to no avail.

Running ACF 5.7.10 as well.

@darrenjacoby

This comment has been minimized.

Copy link
Member

commented Jan 23, 2019

@christianmagill does the error persist if you add an advanced custom field to a category/taxonomy? I haven't experienced this issue on 2.1.1, so any information to try recreate on my end would be helpful.

@darrenjacoby

This comment has been minimized.

Copy link
Member

commented Feb 6, 2019

@christianmagill following up here

@dotsam

This comment has been minimized.

Copy link
Contributor

commented Feb 19, 2019

I'm able to duplicate this on the 404 page, but not on an archive page.

The behavior I'm seeing on a 404 page seems to be completely an ACF bug, as I can replicate it on a stock WP 5.0.3 install w/ ACF 5.7.12 and the twentynineteen theme. Calling ACF's get_field_objects() on a 404 page here produces the same error. ACF has some logic that defaults to 'post' with an ID of 0 or null, and then assumes it's been able to get metadata for that combination and tries to foreach on it without checking if it's an array.

I'll file a bug report with ACF, but if you wanted to avoid this in controller, I would think a check for $query being false-y and then not running get_fields() is a reasonable workaround.

As to the issue occurring on a taxonomy/archive page, I haven't been able to replicate that either with controller or with a vanilla install.

@christianmagill

This comment has been minimized.

Copy link
Author

commented Feb 22, 2019

@darrenjacoby Sorry for the late reply, I've been off of the related project for a bit.

The issue does not appear on category/taxonomy pages, but it does also appear on the front/index view when a page is not assigned.

I'm also running into the same issue on a new project with fresh roots/sage installation.

@mmirus

This comment has been minimized.

Copy link

commented Mar 1, 2019

Replicated on 404 and on a CPT archives.

Temporarily worked around it like this:

App.php

<?php

namespace App\Controllers;

use Sober\Controller\Controller;

class App extends Controller
{
    protected $acf = false;

    public function __construct()
    {
        if (acf_get_valid_post_id(get_queried_object())) {
            $this->acf = true;
        }
    }

// ...

My understanding is that get_fields accepts any of the following:

  • post ID
  • post object
  • user object
  • term object
  • comment object
  • the strings 'option' or 'options'

Controller passes get_queried_object() to get_fields(). In the case of a 404, get_queried_object() returns NULL, and for a CPT archives, it returns a WP_Post_Type object, neither of which are valid values.

Hope this helps towards a resolution!

@christianmagill

This comment has been minimized.

Copy link
Author

commented Mar 3, 2019

@mmirus I believe your work around while preventing the error, would also prevent acf option fields from being loaded as well.

@mmirus

This comment has been minimized.

Copy link

commented Mar 4, 2019

@christianmagill - I just noticed that what I'm seeing is not 100% the same as what you're experiencing. I'm not actually getting an error, just a warning, and my message traces back somewhere slightly different:

Warning: Invalid argument supplied for foreach() in /mnt/c/Users/mmirus/valet/thelibreinitiative/wp-content/plugins/advanced-custom-fields-pro/includes/acf-meta-functions.php on line 95

They do still seem to share some characteristics, though, and probably the same root cause.

Looks like you're correct about my workaround dropping the options fields--good catch. If losing the options isn't an... option... for you, you can find an alternate approach (please share, if you do!) or downgrade to 2.1.0 until Darren has a chance to figure this out. I believe this was introduced in 2.1.1, so if you don't need the other changes in that update, you can just drop back a version for now.

@mmirus

This comment has been minimized.

Copy link

commented Mar 4, 2019

You might also be able to use my workaround and then just manually fetch the options in your controller with something like this:

<?php

declare(strict_types=1);

namespace App\Controllers;

use Sober\Controller\Controller;

class App extends Controller
{
    protected $acf = false;

    public function __construct()
    {
        if (acf_get_valid_post_id(get_queried_object())) {
            $this->acf = true;
        }
    }

    public function acfOptions() : array
    {
        return get_fields('options') ?: [];
    }
@darrenjacoby

This comment has been minimized.

Copy link
Member

commented Mar 4, 2019

Sorry everyone, I'll get this fixed as soon as I get a chance! Been swamped under last month so still catching up a little.

@mmirus

This comment has been minimized.

Copy link

commented Mar 4, 2019

No worries, take your time. Appreciate all the work you've put into Controller to make it such a useful tool for us.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.