Support

Account

Home Forums Front-end Issues pre_get_posts function conflict with ACF Field

Solved

pre_get_posts function conflict with ACF Field

  • Hi forum,

    i ran into an error i can’t figure out.

    i have a ACF Options page for my Theme Logo field (returns Array)

    it works on every page template except of my archive-cpt.php

    i try to sort my cpt by acf field and have this in my functions file

    function sort_by_date_termin( $query ) {
       if ( is_post_type_archive( 'termin') ) {
          $query->set('meta_key', 'datum');
          $query->set('orderby', 'meta_value_num');
          $query->set('meta_type', 'DATE');
          $query->set('order', 'ASC');
          return;
       }
    }
    add_filter( 'pre_get_posts', 'sort_by_date_termin', 1);

    when i remove this filter the archive-cpt.php gives no errors and the theme logo is normally displayed in the header

    when the filter is active i get this error:

    <b>Warning</b>: Illegal string offset ‘url’ in <b>/www/htdocs/domain.com/wp-content/themes/mythemename/header.php</b> on line <b>24</b>

    Warning</b>: Illegal string offset ‘alt’ in <b>/www/htdocs/domain.com/wp-content/themes/mythemename/header.php</b> on line <b>24</b>

    so there must be some conflict with the filter pre_get_posts

    this is line 24 in my header.php

    $mainlogo = get_field('header_logo' , 'option'); ?>
    <img src="<?php echo $mainlogo['url']; ?>" alt="<?php echo  $mainlogo['alt']; ?>">
  • Solved it with this so only main query is affected.

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

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

You must be logged in to reply to this topic.