Site logo

Archived topic

Fatal PHP error after latest Generatepress Premium update

2 replies · Started by Francesco on March 6, 2021

Viewing posts 1–3 of 3

Hi,

I just updated to the last Generatepress Premium Theme, and suddenly I get fatal PHP errors on homepage and archives:
fatal error: Uncaught Error: Call to undefined function get_field() in /***/wp-content/plugins/code-snippets/php/snippet-ops.php(446)

MY functions are generated through code-snippets plugin, below is the code that stopped working after the theme update 2 days ago, and I did not change anything else on the website.

add_action( 'pre_get_posts', 'frl_exclude_posts_from_archive' );

function frl_get_excluded_terms( $field, $type='post_tag' ) { 
	if ( is_admin() || !function_exists('the_field') ) return array();
	$exclude_terms = array();
	
	$terms = is_single() ? get_tags() : get_terms( array('taxonomy' => $type) );
	foreach ( $terms as $term ) {			
		$hide = get_field( $field, $term );			
		if ( isset($hide) && $hide === TRUE ) $exclude_terms[] = $term->term_id;
	}
	return $exclude_terms;
}
function frl_exclude_posts_from_archive( $query ) { 
	if ( ( !is_admin() && ( is_front_page() || is_home() || is_page('blog') || is_page('articoli') || is_category() || is_tag() ) ) !== TRUE ) return;
	
	$exclude_tags = frl_get_excluded_terms( 'hide_tag_posts' );
	if( is_tag() ) {
		$term = get_queried_object()->term_id;
		if ( ($key = array_search($term, $exclude_tags)) !== false ) unset($exclude_tags[$key]);
	}
	if( !empty( $exclude_tags ) ){
		$tax_query =array();
		$tax_query[] = $query->get('tax_query');		
		$tax_query[] = array(
			'relation' => 'OR',
			array(
				'taxonomy' =>'post_tag',
				'terms'    => $exclude_tags,
				'operator' => 'NOT IN',
			),
			array(
				'taxonomy' => 'category',
				'terms'    => array(1),
				'operator' => 'NOT IN',
			),
		);
		$query->set( 'tax_query', $tax_query );
	}
}

On an important note:
1) If I comment the following line in the function "frl_get_excluded_terms()" the error disapperars:
// $hide = get_field( $field, $term );

2) I have other 2 functions calling the same function "frl_get_excluded_terms()", which work fine instead:

add_filter( 'widget_tag_cloud_args', 'frl_hide_tags_from_tag_cloud');
add_filter( "term_links-post_tag", 'frl_hide_tags_from_post' );

function frl_hide_tags_from_tag_cloud( $args ) {
	$args[ 'exclude' ] = frl_get_excluded_terms( 'hide_tag' ); 
  	return $args;
}
function frl_hide_tags_from_post ( $term_links ) {
	$hide_tags_names = array();
	$result = array();
	
    $hide_tags = frl_get_excluded_terms( 'hide_tag' );
	foreach ( $hide_tags as $tag ) {
		$tag_obj = get_tag( $tag );
		if($tag_obj) $hide_tags_names[] = $tag_obj->name;
	}
	foreach ( $term_links as $link ) {
        foreach ( $hide_tags_names as $tag ) {
			if ($tag && stripos( $link, $tag ) !== false ) continue 2;
        }
        $result[] = $link;
    }
    return $result;
}

Please help.
Francesco

Hi there,

get_field() isn't a GeneratePress function - I believe it's an ACF function. Is ACF installed and activated on your website?

Hi Tom, thanks for your reply.

Yes I have ACF, I gave it as implied. :-)

I ask your support because so far ACF worked well and the issue started in correspondence of updating the generatepress theme. So I thought you might help.

Kind regards, Francesco

This archived topic is closed to new replies.