Array to string conversion in the set_archive_image() function

#78679
  • Resolved Bastian Fießinger
    Rank Math free

    This ticket is related to #52072.

    When we are on a search page there are multiple post types in the query_vars (this might also happen in some other cases).

    `
    $post_type = get_query_var( ‘post_type’ );
    $image_id = Helper::get_settings( “titles.pt_{$post_type}_facebook_image_id” );
    `

    Currently in such a case the plugin tries to print the var on line 518 in /includes/opengraph/class-image.php
    Maybe there should be a check for is_array( $post_type ) before and if so it should either do nothing or use the first post_type from the array.

Viewing 8 replies - 1 through 8 (of 8 total)
  • Edit:
    I have noticed this only happens if Content type is Archive search.
    This means if your search url is domain.com/cstm-posttype-archive/?s=str or domain.com/?s=str&post_type=my_custom_post_type

    Hello,

    Thank you for getting in touch with us.

    We are sorry for the late reply but we have sent this to our dev. team and you will get a reply ASAP.

    Appreciate your patience in this matter.

    Hello,

    The set_archive_image() function is only called on the Post type archive page so it’s strange that you are getting this error on the Search page. Have you added any code on your site to customize the Search page queries in other pages or are you using any plugin for that?

    Looking forward to helping you.

    hey @pratik-d,

    yes the query is modified.

    
    if ( helper::getInstance()->is_wiki_search( $query ) ) {
    	$query->set( 'post_type', post_type::getInstance()->post_type );
    }
    

    So if I search inside the archive I set the queries post_type to only the post_type that should be searched.

    Hello,

    Sorry for not following up quickly and any inconvenience that might have been caused due to that.

    Before adding it as an issue in our internal todo lists, I would like to make sure if it’s something we can add in our plugin or if we can provide any workaround to you. So to better understand the issue, can you please add your site’s admin & FTP access in the Sensitive Data Section? Also please let us know where you have added this code to change the post_type query variable.

    Looking forward to helping you.

    The site is on localhost.

    Here’s some code relevant for the issue.
    In the pre_get_posts action I set post_type var in WP_Query.

    
    namespace bf\wpPedia;
    
    use bf\wpPedia\helper;
    use bf\wpPedia\post_type;
    
    // Make sure this file runs only from within WordPress.
    defined( 'ABSPATH' ) or die();
    
    class query_control {
    
      ...
    
      protected function __construct() {
    
    	...
    
    	// Allow searching in glossary entries only
    	add_filter( 'pre_get_posts', [ $this, 'search_wppedia' ], 202 );
    
            ...
      }
    
      ...
    
      /**
       * Modify the main query for searches on the archive page
       * 
       * @since 1.0.0
       */
      function search_wppedia( $query ) {
    
        if ( helper::getInstance()->is_wiki_search( $query ) ) {
    	$query->set( 'post_type', post_type::getInstance()->post_type );
        }
    
        return $query;
    
      }
    
      ...
    
    }
    

    is_wiki_search function:

    
    	/**
    	 * Determine if the current view is a wiki search
    	 * 
    	 * @uses is_wiki_post_type()
    	 * 
    	 * @since 1.0.0
    	 */
    	public function is_wiki_search( $query = false ) {
    
    		if ( ! $query ) {
    			global $wp_query;
    			$query = $wp_query;
    		}
    
    		if ( $query->is_search() && $this->is_wiki_post_type( $query ) )
    			return true;
    		
    		return false;
    
    	}
    

    is_wiki_post_type function:

    
    	/**
    	 * Determine if the currently viewed page is a wiki page
    	 * 
    	 * @since 1.0.0
    	 */
    	public function is_wiki_post_type( $query = false ) {
    
    		if ( ! $query ) {
    			global $wp_query;
    			$query = $wp_query;
    		}
    
    		/**
    		 * No Checks should be performed if the request is 404
    		 * or we are not on the main query
    		 */
    		if ( is_404() || ! $query->is_main_query() )
    			return false;
    
    		$post_type = post_type::getInstance()->post_type;
    		$is_wppedia_post_type = false;
    
    		global $wp;
    
    		if ( 
    			/**
    			 * Check for singular and archive pages where there is only
    			 * one given post type
    			 */
    			(
    				! $query->is_search() &&
    				get_post_type() == $post_type
    			) ||
    			/**
    			 * Check for searches in the archive
    			 */
    			(
    				$query->is_post_type_archive() &&
    				rtrim( home_url( $wp->request ), '/' ) == rtrim( get_post_type_archive_link( $post_type ), '/' )
    			) ||
    			/**
    			 * Check for requests to the custom selected static WPPedia front page
    			 */
    			get_the_ID() === intval( $this->has_static_archive_page() )
    		)
    			$is_wppedia_post_type = $post_type;
    			
    		return $is_wppedia_post_type;
    
    	}
    

    maybe this helps to understand how the error is happening.

    Oh and the post_type var is of course just a string containing the name of the post_type.

    Hello,

    Sorry for not following up quickly and any inconvenience that might have been caused due to that.

    The code you provided, is it from any plugin or theme you created? If so, can you share the entire plugin/theme files with the steps to reproduce the issue?

    From what I understood, on search page, you are changing the post_type using the pre_get_posts filter but that shouldn’t cause any problem. I tried changing it on my setup too and it worked fine.

    Looking forward to helping you.

    Hello,

    Since we did not hear back from you for 15 days, we are assuming that you found the solution. We are closing this support ticket.

    If you still need assistance or any other help, please feel free to open a new support ticket, and we will be more than happy to assist.

    Thank you.

Viewing 8 replies - 1 through 8 (of 8 total)

The ticket ‘Array to string conversion in the set_archive_image() function’ is closed to new replies.