Support

Account

Home Forums Bug Reports get_field image returns ID only on search results page (PRO) Reply To: get_field image returns ID only on search results page (PRO)

  • Whenever this happens, 99% of the time is has to do with a pre_get_posts filter filtering something it shouldn’t. All of the examples that you’ll find, or at least most of them that I’ve seen, do not take into account that the filter is run on every query done in WP and the examples to not include information on making sure you don’t apply the filter to things that you should not. The reason that the examples don’t is because, well, that information is not easy to come by and takes a bit of trial and error.

    This is where the PHP error log comes in handy.

    
    function mine_add_custom_types( $query ) {
      ob_start();
      print_r($query);
      error_log(ob_get_clean());
    }
    add_filter( 'pre_get_posts', 'mine_add_custom_types' );
    

    This will put every query done when loading a page into the PHP (WP) error log. You can then examine the error logs and figure out how to target only the query you want to alter.

    Although, in the case of the search results and adding post types is to make sure you only apply your filter to “The Main Query”

    
    function mine_add_custom_types( $query ) {
      if (!$query->is_main_query()) {
        return;
      }
      // the rest of your code
    }
    add_filter( 'pre_get_posts', 'mine_add_custom_types' );