Home Forums General Issues Issues with pre_get_posts() and get_field()


Issues with pre_get_posts() and get_field()

  • Hi,

    I have done some searching but couldn’t find anything that matched my particular issue.

    I am having issues with ACF when using the pre_get_posts filter in my functions.php file.

    I have following code that combines two posts types into the standard archive page.

    function include_tweets_in_news_archive( $wp_query ) {
        if ( !is_admin() && is_home() ) {
            $wp_query->set( 'posts_per_page', 12 );
            $wp_query->set( 'post_type', array( 'post', 'tweet' ) );
            $wp_query->set( 'order', 'DESC' );
    add_action( 'pre_get_posts', 'include_tweets_in_news_archive' );

    Then on my archive page I am calling get_field to display a number of images that are created in ACF for an options page. The issue is is that regardless of what the return type for these image fields is set as (array, ID or URL) it always returns the image ID as a string.

    On every other page that displays these fields it works fine and if I comment out that function it works fine on the archive page too, but I was wondering if anyone had experienced a similar issue or knew why it was happening.

    For the time being I am checking whether or not it is a URL and if not then assuming it is an ID and loading the image that way, but this seems like a bug.


  • The problem is that your changes to the query are effecting every query, including those to get attachments, fields, field groups, anything that is appearing on the home page. You need to narrow down what you are applying this to more than you have.

    For example, if this is to alter the query that shows the archive list of “Posts” in order to add “Tweets” then you should check to make sure that the post type being queried is in fact “post”

    for example:

    if ( !is_admin() && is_home() && 
          isset($wp_query->query_vars['post_type'] && 
          $wp_query->query_vars['post_type'] == 'post') {
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Issues with pre_get_posts() and get_field()’ is closed to new replies.