get thumbs from serialied array

Home Forums Product Support Forums Ajax Search Pro for WordPress Support get thumbs from serialied array

This topic contains 14 replies, has 2 voices, and was last updated by iloop19 iloop19 3 years, 4 months ago.

Viewing 15 posts - 1 through 15 (of 15 total)
  • Author
    Posts
  • #28339
    iloop19
    iloop19
    Participant

    Hello

    As you told me in comments before purchase, could pleae give a snippet to get the thumbnail from a serialized array ?
    https://codecanyon.net/item/ajax-search-pro-for-wordpress-live-search-plugin/3357410/comments

    a:1:{i:0;s:105:”https://mydomain.com/wp-content/uploads/listing-uploads/logo/2020/04/Logo-F-Bracken.jpg”;}

    but it should work with a simple url field: https://mydomain.com/wp-content/uploads/listing-uploads/logo/2020/04/Logo-F-Bracken.jpg

    thanks a lot

    #28342
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi,

    Try adding this custom code to the functions.php in your theme/child theme directory. Before editing, please make sure to have a full site back-up just in case!

    add_filter( 'asp_results', 'asp_custom_image_results', 10, 1 );
    function asp_custom_image_results( $results ) {
    	$field_name = 'field_name';
    	// Parse through each result item
    	foreach ($results as $k=>&$r) {
    		if ( $r->content_type == 'pagepost' && empty($r->image) ) {
    			$field = get_post_meta( $r->id, $field_name , true );
    			$field = maybe_unserialize($field);
    			if ( isset($field[0]) )
    				$r->image = $field[0];
    		}
    	}
    
    	return $results;
    }

    Make sure to change the $field_name variable to the custom field name, that holds the image.

    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #28343
    iloop19
    iloop19
    Participant

    Hi,

    Thanks a lot for your code unfortunately i can’t make it work.

    My field is _job_logo, but the thumb does not appear.

    Best regards,

    Attachments:
    You must be logged in to view attached files.
    #28347
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Can you please add temporary FTP and back-end details? I would like to debug the code to see what exactly is returned there. Thank you!

    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #28348
    iloop19
    iloop19
    Participant

    i can prepare you that but this is what is returned in $results:

    Array
    (
    [0] => stdClass Object
    (
    [title] => JUSTINE LABESSE
    [post_title] => JUSTINE LABESSE
    [id] => 2908
    [blogid] => 1
    [date] => 2018-09-07 08:26:08
    [post_date] => 2018-09-07 08:26:08
    [content] => Conceptrice-rédactrice et consultante marketing.

    Vous faire exister et grandir dans le coeur de vos clients !

    J’aide les…
    [excerpt] =>
    [post_type] => job_listing
    [content_type] => pagepost
    [g_content_type] => post_page_cpt
    [author] => hello
    [post_author] => 26
    [priority] => 100
    [p_type_priority] => 1
    [group_priority] => 1
    [relevance] => 20
    [customfp] => 1
    [customfs] => 1
    [image] => Array
    (
    [0] => https://entreprises-uzes-pontdugard.fr/wp-content/uploads/listing-uploads/logo/2020/02/cartouche_blc.png
    )

    [link] => https://entreprises-uzes-pontdugard.fr/entreprise/justine-labesse/
    )

    )

    #28349
    iloop19
    iloop19
    Participant

    and this is for post that works (when _job_logo is not serialized)

    Array
    (
    [0] => stdClass Object
    (
    [title] => SARL A2CP
    [post_title] => SARL A2CP
    [id] => 7079
    [blogid] => 1
    [date] => 2020-07-01 11:39:22
    [post_date] => 2020-07-01 11:39:22
    [content] => Société réalisation les diagnostics immobiliers réglementaires dans le cadre de la vente et de la location, diagnostics amiante…
    [excerpt] =>
    [post_type] => job_listing
    [content_type] => pagepost
    [g_content_type] => post_page_cpt
    [author] => a2cp
    [post_author] => 1214
    [priority] => 100
    [p_type_priority] => 1
    [group_priority] => 1
    [relevance] => 1
    [customfp] => 1
    [customfs] => 1
    [image] => https://entreprises-uzes-pontdugard.fr/wp-content/uploads/listing-uploads/logo/2020/07/Logo-A2CP-web.jpg
    [link] => https://entreprises-uzes-pontdugard.fr/entreprise/sarl-a2cp/
    )

    [1] => stdClass Object
    (
    [title] => CAP VITAL SANTÉ DE L’UZÈGE
    [post_title] => CAP VITAL SANTÉ DE L’UZÈGE
    [id] => 2872
    [blogid] => 1
    [date] => 2020-06-30 14:17:36
    [post_date] => 2020-06-30 14:17:36
    [content] => Vente et location de matériel médical – Particuliers, Professionnels, Collectivités.

    Orthopédie de série et sur…
    [excerpt] =>
    [post_type] => job_listing
    [content_type] => pagepost
    [g_content_type] => post_page_cpt
    [author] => [email protected]
    [post_author] => 50
    [priority] => 100
    [p_type_priority] => 1
    [group_priority] => 1
    [relevance] => 1
    [customfp] => 1
    [customfs] => 1
    [image] => https://entreprises-uzes-pontdugard.fr/wp-content/uploads/listing-uploads/logo/2020/06/LOGO-CV-1.jpg
    [link] => https://entreprises-uzes-pontdugard.fr/entreprise/cap-vital-sante-de-luzege/
    )

    #28350
    iloop19
    iloop19
    Participant
    #28351
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    That is strange. Maybe this variation:

    add_filter( 'asp_results', 'asp_custom_image_results', 10, 1 );
    function asp_custom_image_results( $results ) {
    	$field_name = 'field_name';
    	// Parse through each result item
    	foreach ($results as $k=>&$r) {
    		if ( $r->content_type == 'pagepost' && empty($r->image) ) {
    			$field = get_post_meta( $r->id, $field_name , true );
    			$field = maybe_unserialize($field);
    			if ( isset($field[0], $field[0][0]) )
    				$r->image = $field[0][0];
    		}
    	}
    
    	return $results;
    }
    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #28352
    iloop19
    iloop19
    Participant

    NOpe…

    It seems this return false so it does no enter in the condition
    if ( $r->content_type == ‘pagepost’ && empty($r->image) )

    #28353
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    If you set the image source on the back-end to that same custom field, then make sure to turn it off, that may cause the issue with that condition.

    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #28354
    iloop19
    iloop19
    Participant

    The code below works ! but now i don’t have image that are not in a sub array.
    not OK:
    [image] => https://entreprises-uzes-pontdugard.fr/wp-content/uploads/listing-uploads/logo/2020/07/Logo-A2CP-web.jpg
    OK:
    [image] => Array
    (
    [0] => https://entreprises-uzes-pontdugard.fr/wp-content/uploads/listing-uploads/logo/2020/02/cartouche_blc.png
    )

    add_filter( ‘asp_results’, ‘asp_custom_image_results’, 10, 1 );
    function asp_custom_image_results( $results ) {
    $field_name = ‘_job_logo’;
    foreach ($results as $k=>&$r) {
    $field = get_post_meta( $r->id, $field_name , true );
    if ( isset($field[0])) {$r->image = $field[0];} }

    return $results;
    }

    #28355
    iloop19
    iloop19
    Participant

    this is very not elegant but working:
    if ( isset($field[0])) {$r->image = $field[0];}
    if ($field[0]==”h”) {$r->image = $field;}

    #28357
    iloop19
    iloop19
    Participant

    please what do you think of the code , could it be better ?
    thanks

    #28361
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    It looks all right, maybe this variation is shorter:

    $r->image = is_array($field) ? $field[0] : $field;

    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #28363
    iloop19
    iloop19
    Participant

    thanks a lot !!

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

You must be logged in to reply to this topic.