Support

Account

Home Forums Add-ons Gallery Field Empty array when ordering posts by meta_value

Solved

Empty array when ordering posts by meta_value

  • I’m using the Gallery field on a custom post type, and displaying a list of those posts on a custom taxonomy term page. The taxonomy is called buzz_type and the term is product, so I’m on the template file taxonomy-buzz_type-product.php.

    When I loop through the posts to display them, get_field('gallery_images') gives me an array of the uploaded images, exactly as it should. However, I wanted to sort the posts by another meta field I created called date_published so I created the following filter:

        function allstar_buzzworthy_published_sort( $query ) {
            // If not the admin section
            if ( !is_admin() ) {
                // If on a taxonomy page
                if ( is_tax() ) {
                    $query->set( 'meta_key', 'date_published' );
                    $query->set( 'orderby', 'meta_value' );
                    $query->set( 'order', 'DESC' );
                }
            }
        }
        add_action( 'pre_get_posts', 'allstar_buzzworthy_published_sort' );

    After applying the filter, the posts are sorted properly, but the get_field('gallery_images') is now giving me an empty array, rather than an array of the images as before. It’s still giving me an array instead of an empty string like it does on posts with no images uploaded, but I’m not getting any of the image data.

    If I remove the line $query->set( 'meta_key', 'date_published' ); the images show up again, but I need to sort the posts in this way so that’s not an option.

    EDIT: I want to note that all my other get_field() queries are working as normal. It’s just the gallery that’s not working.

    • Elliot

    • November 14, 2013 at 4:31 pm

    Hi @ryangiglio

    Thanks for the detailed report.
    To be honest, I have absolutely no idea why this would be happening.

    Within your loop, can you confirm that the $post->ID is correct? Can you test using a more basic WP function such as get_postmeta?

    Is it possible that the posts shown don’t have any gallery data?

    Thanks
    E

  • (This is going to be interesting to work out since we’re in basically opposite time zones)

    I will check the $post-ID in the morning when I get to the office, as well as testing a basic function, but everything else for the post is displaying properly…the name, permalink, even other metadata I’ve set using ACF and am accessing with get_field(). The ONLY thing that isn’t working is that the gallery is returning an empty array rather than an array of images.

    I am certain that the posts have gallery data, because on their individual pages the gallery is being displayed fine, and when I remove my pre_get_posts filter, the galleries show up on the correct posts.

    Thanks,
    Ryan

    • Elliot

    • November 14, 2013 at 4:44 pm

    Hi @ryangiglio

    Totally random! Hopefully you can find the solution with some simple debugging.

    Keep me posted.

    Cheers
    E

  • If you have any other suggestions of debugging I should try I’d definitely love to hear them. I’m afraid it might be some kind of bug in the way that ACF and WP interact in this extremely specific instance and I don’t have a terribly large amount of time to dig around in Core…this is on a client site with a fairly tight deadline.

  • I am also seeing the same behaviour after upgrading to Gallery 1.1.0 and ACF 4.3.0.

    With add_action('pre_get_posts'... called in functions.php, get_field for a gallery field returns an empty array.

    If I comment out the pre_get_posts action everything works as expected.

  • What are you doing inside your pre_get_posts action? I don’t believe it’s the pre_get_posts action that’s causing the problem. If I just have it echo out some text, get_field returns an array of images as expected. It’s when I modify the query by adding $query->set( 'meta_key', date_published' ); that I run into a problem.

  • I’m digging into the gallery.php code a little bit and I have a thought as to what the problem is. Let me know if this makes any sense.

    This is the code to get the attachments form the database:

    // find attachments (DISTINCT POSTS)
    $attachments = get_posts(array(
        'post_type' => 'attachment',
        'numberposts' => -1,
        'post_status' => null,
        'post__in' => $value,
    ));

    Is it possible that my $query->set( 'meta_key', 'date_published' ); is affecting this query also, since it’s using get_posts instead of making a brand new WP_Query? So in my case, it’s trying to order the attachments by date_published and since that doesn’t exist, it’s not returning anything? Will investigate further.

  • The issue for me was specific to $query->set('s', ' ');. I added it to fix empty search queries returning a 404 page.

    I set a bunch of other query vars in the same function but going through one by one, this was the only one to cause error.

    Btw, you’re missing a single quote before date_published.

      
    function property_filters($query) {
    
      // ...
    
      /**
       * Fixes issue with blank search queries returning 404
       */
      if (!is_admin() && isset($query->query_vars['s']) && empty($query->query_vars['s'])) {
        $query->set('s', ' ');
      }
    }
    add_action('pre_get_posts', 'property_filters');
    
  • Fortunately the typo was only in my comment here and not in my code. That’d have been infuriating!

  • I did some debugging in gallery.php and found that the issue his happening in the format_value function. The $value variable has the IDs of the attachments as it should, but when the get_posts query is run to get the data from those attachments, nothing is returned. I’m thinking my theory about the $query->set calls messing with the get_posts query. I’m going to see if I can tweak it and make it work.

  • What happens if you add $query->is_main_query() to your if statement?

    
    function allstar_buzzworthy_published_sort( $query ) {
            // If not the admin section
            if ( !is_admin() &&  $query->is_main_query()) {
                // If on a taxonomy page
                if ( is_tax() ) {
                    $query->set( 'meta_key', 'date_published' );
                    $query->set( 'orderby', 'meta_value' );
                    $query->set( 'order', 'DESC' );
                }
            }
        }
        add_action( 'pre_get_posts', 'allstar_buzzworthy_published_sort' );
    
  • I just came across that at the same time that you posted it. That works! Thank you!

  • Just to clarify for anyone who comes across this later:

    My pre_get_posts action was setting query parameters not just for the main query (as I intended) but also the query by the Gallery plugin that gets the attachment data. These query parameters caused the get_posts query in gallery.php to return nothing.

Viewing 14 posts - 1 through 14 (of 14 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.