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 3 years, 4 months ago.
- AuthorPosts
- July 9, 2020 at 12:53 pm #28339
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/commentsa: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
July 9, 2020 at 2:06 pm #28342Hi,
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 :)
July 9, 2020 at 2:22 pm #28343Hi,
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.July 9, 2020 at 2:26 pm #28347Can 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 :)
July 9, 2020 at 2:32 pm #28348i 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/
))
July 9, 2020 at 2:34 pm #28349and 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/
)July 9, 2020 at 2:38 pm #28350so we have either this:
either with an array:
[image] => Array
(
[0] => https://entreprises-uzes-pontdugard.fr/wp-content/uploads/listing-uploads/logo/2020/02/cartouche_blc.png
)July 9, 2020 at 3:01 pm #28351That is strange. Maybe this variation:
Best,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; }
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
July 9, 2020 at 3:15 pm #28352NOpe…
It seems this return false so it does no enter in the condition
if ( $r->content_type == ‘pagepost’ && empty($r->image) )July 9, 2020 at 3:16 pm #28353If 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 :)
July 9, 2020 at 3:44 pm #28354The 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;
}July 9, 2020 at 4:16 pm #28355this is very not elegant but working:
if ( isset($field[0])) {$r->image = $field[0];}
if ($field[0]==”h”) {$r->image = $field;}July 9, 2020 at 6:14 pm #28357please what do you think of the code , could it be better ?
thanksJuly 10, 2020 at 8:20 am #28361It looks all right, maybe this variation is shorter:
Best,$r->image = is_array($field) ? $field[0] : $field;
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
July 10, 2020 at 8:31 am #28363thanks a lot !!
- AuthorPosts
You must be logged in to reply to this topic.