Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › Custom field in search results
- This topic has 7 replies, 2 voices, and was last updated 7 years, 6 months ago by
Ernest Marcinko.
-
AuthorPosts
-
November 26, 2015 at 1:17 pm #6793
tim
ParticipantHi
Everything is running, but I’m still working on a couple of issues. Hope you can help me with these:
– There is a custom field that says ‘I’m an part of another publication’ (like a volume): “Deel van”. But in viewing a single publication in front-end there is only displayed “array”. For example: catbibjugt.be/publicatie/koop-en-verkoop-van-onroerende-goederen-modellen-en-commentaar-2/ . Any ideas how I can fix this? I’want to show all the linked publications.
– In the searchresults I would like to show the authors (a custom field “auteur”) below the title (not the author according wordpress, but a custom field). Any ideas how I can achieve this?
Thanks!
Tim
November 26, 2015 at 3:48 pm #6794Ernest Marcinko
KeymasterHi Tim,
It’s nice to hear from you 🙂
1. Well this issue is not related to the search, but I guess I can help you with it. So I’ve notice you are using the single-publicatie.php file in your theme folder to display these values. The “Array” output there means that it’s not a single value stored but an array of something. After some debugging I found that it’s an array of the post ID in it, so I’ve changed the code on line 51 to this:
[php]
<p>Deel van: <?php
$related_p_ids = get_post_meta($post->ID, ‘deel_van’, true);
$to_echo = array();
foreach ($related_p_ids as $pid) {
$to_echo[] = "<a href=’".get_permalink($pid)."’>" . get_the_title($pid) . "</a>";
}
if ( count($to_echo) > 0 )
echo implode(", ", $to_echo);
?></p>
[/php]What this does:
– Gets the array of the ids to the $related_p_ids variable
– Gets the permalink and title and forms a link element, then adds it to the $to_echo array
– Implodes the $to_echo array with a comma “, ” delimiter, in case multiple posts were found to print nicely
– Prints the links with on the same line.2. With a filter function easily. I’ve already appended this to your functions.php file:
[php]
add_filter( ‘asp_result_author_after_prostproc’, ‘asp_author_from_cf’, 1, 2);function asp_author_from_cf($author, $post_id) {
$cf_author = get_post_meta($post_id, ‘auteur’, true);
if ($cf_author != false)
return $cf_author;
return $author;
}
[/php]Right now the “auteur” fields are empty, but if you start adding text there, it will appear instead of the default author field. Don’t forget to turn back on the “Show author in results” option on the Layout options panel as well.
November 26, 2015 at 10:20 pm #6796tim
ParticipantHi Ernest
You are the best!
Nr 1. works perfect! But any suggestions to place multiple results below each other?
Only nr. 2 isn’t working: the default author field still appears. The “auteur” field is in fact a taxonomy; maybe that changes things?
Thanks again for the great support.
Best,
Tim
December 8, 2015 at 10:30 am #6949Ernest Marcinko
KeymasterHi!
1. Try changing that to this:
[php]
<p>Deel van: <?php
$related_p_ids = get_post_meta($post->ID, ‘deel_van’, true);
$to_echo = array();
foreach ($related_p_ids as $pid) {
$to_echo[] = "<a href=’".get_permalink($pid)."’>" . get_the_title($pid) . "</a>";
}
if ( count($to_echo) > 0 )
echo implode("<br>", $to_echo);
?></p>
[/php]This puts a line break inbetween items.
2. Well, in this case you can try something like:
add_filter( ‘asp_result_author_after_prostproc’, ‘asp_author_from_cf’, 1, 2);
function asp_author_from_cf($author, $post_id) {
[php]
$authors = get_the_terms( $post_id, "auteur" );
$author_names = array();
if ( is_array($authors) ) {
foreach ($authors as $a) {
$author_names[] = $a->name;
}
$author = implode(" | ", $author_names);
}return $author;
}
[/php]This should get all authors and if finds any, then adds them all to the author field separated by “I”.
3. As for the term order. I think I have a solution for you 🙂
After researching I found one plugin, that works. Install this plugin: https://wordpress.org/plugins/post-terms-order/It’s working great, it lets me re-order every taxonomy term for every post. Here is how I did it: http://i.imgur.com/ta2qJRx.png
It’s a bit time consuming to do it for every post, but it works flawelessely.December 8, 2015 at 3:25 pm #6951tim
ParticipantHi Ernest
Thank you very much! You solved it all :).
Donation coming your way!
Best,
Tim
December 8, 2015 at 5:09 pm #6953Ernest Marcinko
KeymasterGreat!
I’m glad it’s working 🙂
November 29, 2018 at 2:47 pm #20183tim
ParticipantHi Ernest
Quick question: is it possible to show the filtering above the results?
Thanks,
Tim
November 29, 2018 at 4:18 pm #20190Ernest Marcinko
KeymasterHi Tim,
You can use the settings and results shortcode to position them on the same page, as you like. In your case, you could use a combination like so:
[wpdreams_ajaxsearchpro id=1] [wpdreams_asp_settings id=1 element='div'] [wpdreams_ajaxsearchpro_results id=1 element='div']That should do it.
-
AuthorPosts
- The topic ‘Custom field in search results’ is closed to new replies.