Dave's WordPress Live Search
[resolved] Custom field in front of title (6 posts)

  1. espider
    Posted 4 years ago #

    Hi all,

    Could someone please give me a clue how to add a custom field php code to jQuery? I need to add:
    <?php $key="price"; echo get_post_meta($post->ID, $key, true); ?>

    at the begenning ot this search result:
    renderedResult += '<a href="' + searchResult.permalink + '">' + searchResult.post_title + '</a>';

    Thanks a lot for any help!


  2. Dave Ross
    Plugin Author

    Posted 3 years ago #

    If you grab a copy of version 3.0a2, there's a "dwls_post_title" filter you can use in your theme's functions.php file or a custom plugin to change the title.

    You can get the latest "alpha" (although it's pretty stable & running on production sites) from http://wordpress.org/extend/plugins/daves-wordpress-live-search/download/

    - Dave

  3. BriceLucas
    Posted 3 years ago #

    Hi espider, first you need to read the value in wordpress into a variable for jQuery:

    Note: My meta-field key is "_museum_city" in the below case.

    $meta_city = get_post_meta( get_the_ID(), '_museum_city', true );
    $result->post_city = $meta_city;

    Then, in your JS file:

    if(searchResult.post_city != undefined) { renderedResult += '<p class="price">' + searchResult.post_city + '</p>'; }

    Hope this helps!

  4. espider
    Posted 3 years ago #

    Hi Bruce, thanks hips for your help.

    I managed to display prices in the search results but it shows the same price for all results unless I go to the next post and then the price changes for the current post :) What I did is:

    1) added a new function to DaveWordPressLiveSearchResults.php

    function my_jquery_var() {
        global $post;
        if ( $my_custom_field_name = get_post_meta( $post->ID, 'price', true ) ) {
            echo '<script type="text/javascript">var my_custom_field_name = "' . $my_custom_field_name . '";</script>' . "\n";
    add_action( 'wp_head', 'my_jquery_var' );

    2) added new renderResult in dave-wordpress-live-search.js

    renderedResult += '<a href="' + searchResult.permalink + '">' + '<span class="price_search">' + my_custom_field_name + '</span>' + searchResult.post_title + '</a>';

    Once again, thanks big time for your help

  5. BriceLucas
    Posted 3 years ago #

    Nice espider! This is the exact same problem that I am running into (the value is repeating for every result)! Can you please paste your entire code, or send me the source so that I can figure this out? I am trying to put your solution in, but am having trouble.

    Here is my code (DavesWordpressLiveSearchResults.php):

    //foreach($posts as $result)
    			// Get the ID of a given category
    			$category_list = get_the_term_list( get_the_ID(), 'portfolio_category', "", " - " );
    			$category_link = get_category_link( $category_id );
    			$result->post_category = $category_edit;
        // Get the URL of this category
    			function my_jquery_var() {
        global $post;
        if ( $meta_city = get_post_meta( $post->ID, '_museum_city', true ) ) {
            echo '<script type="text/javascript">var meta_city = "' . $meta_city . '";</script>' . "\n";
    	add_action( 'wp_head', 'my_jquery_var' );
    			// Add author names & permalinks

    and the JS code:

    renderedResult += '<p>' + meta_city + '</p>';

  6. BriceLucas
    Posted 3 years ago #

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • Dave's WordPress Live Search
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic