Custom field in search results

Home Forums Product Support Forums Ajax Search Pro for WordPress Support Custom field in search results

This topic contains 7 replies, has 2 voices, and was last updated by Ernest Marcinko Ernest Marcinko 3 years, 10 months ago.

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #6793
    tim
    tim
    Participant

    Hi

    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

    #6794
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi 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:

    
    <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>
    

    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:

    
    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;  
    }
    

    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.

    Best,
    Ernest Marcinko

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


    #6796
    tim
    tim
    Participant

    Hi 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

    #6949
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi!

    1. Try changing that to this:

    
    <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>
    

    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) {

    
      $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; 
    }
    

    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: https://i.imgur.com/ta2qJRx.png
    It’s a bit time consuming to do it for every post, but it works flawelessely.

    Best,
    Ernest Marcinko

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


    #6951
    tim
    tim
    Participant

    Hi Ernest

    Thank you very much! You solved it all :).

    Donation coming your way!

    Best,

    Tim

    #6953
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Great!

    I’m glad it’s working 🙂

    Best,
    Ernest Marcinko

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


    #20183
    tim
    tim
    Participant

    Hi Ernest

    Quick question: is it possible to show the filtering above the results?

    Thanks,

    Tim

    #20190
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi 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.

    Best,
    Ernest Marcinko

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


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

The topic ‘Custom field in search results’ is closed to new replies.