Support

Account

Home Forums General Issues get_field('my_field_name', $post->ID) returning string on author.php

Solved

get_field('my_field_name', $post->ID) returning string on author.php

  • I have a CPT that I am trying to set up an author archive for. On the regular ol’ archive for that post, I am able to get the field and use its contents on the archive page. On the author archive, whether I am using:

    get_field('my_field_name')

    or

    get_field('my_field_name', $post->ID)

    The return is not the field’s contents but rather:

    string(4) "6124"

    In order to get the CPT on the archive I am using the following filter in my funtions.php:

    
    function wpbrigade_author_custom_post_types( $query ) {
      if( is_author() && empty( $query->query_vars['suppress_filters'] ) ) {
        $query->set( 'post_type', array(
         'post', 'know',
    		));
    	  return $query;
    	}
    }
    add_filter( 'pre_get_posts', 'wpbrigade_author_custom_post_types' );
    ?>
    

    My hunch is that the way I am modifying the $query is the root of the issue here, but honestly, I have no idea. Does anyone see what I’m doing wrong here? Thanks in advance for the help!

  • Add this line:

    if( !$query->is_main_query() ) return;

    at the beginning of you filter, solved it for me

  • @laju is likely correct. The value that your getting (a string value of what appears to be an ID) indicates that you’re using some type of relationship field and your pre_get_posts filter is interfering with the query that ACF is doing. The following should be added to all pre_get_posts filters that are designed to filter the main query.

    
    if (is_admin())  || !$query->is_main_query()) {
      return;
    }
    

  • @laju
    and @hube2, thank you both so much for the help!

    if( !$query->is_main_query() ) return;

    Did the trick for me! Adding in the or is_admin() threw an error, so I will be leaving that portion out.

    Thanks again to both of you! I was near keyboard smashing levels of hung up on this one. You both rock!

  • 😛 There is an extra ) after is_admin()

    Sorry, there is no syntax checker (which I depend on when coding) when adding code here.

  • Ahhh, yes. I figured it was a typo that if I used my brain for two seconds could be discovered, but why use my brain when the functionality I was seeking was achieved with the less complete conditional? I added that extra ) and it’s working beautifully now. Thanks again for the help @hube2!

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

You must be logged in to reply to this topic.