I want to use post_id in Advanced Description field

Home Forums Product Support Forums Ajax Search Pro for WordPress Support I want to use post_id in Advanced Description field

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

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #26195
    awaistufail02
    awaistufail02
    Participant

    I want to compare different products on a compare-products page. For searching and adding products in compare list I’m using Ajax Search Pro and in order to add products in compare list I want Advanced Description Field to be like this:


    <br>
    {titlefield}

    Everything else is working just fine but I’m not able to get the post_id of the product being displayed in search list. Need your help.

    #26196
    awaistufail02
    awaistufail02
    Participant

    oops! I should have put it in CODE

    
    <a href="http://pkmobilereviews.com/compare/?post_id={post_id}">
    {__image}
    {titlefield}
    </a>
    
    #26203
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi!

    The post ID is not available via those fields unfortunately, but it should be doable via a simple custom code. First, change the advanced content field to this:

    <a href="http://pkmobilereviews.com/compare/?post_id=|post_id|">
    {__image}
    {titlefield}
    </a>

    Then, 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_change_content_post_id', 10, 4 );
    function asp_change_content_post_id($results, $search_id, $is_ajax, $args) {
    	foreach ( $results as $k => &$r ) {
    		$r->content = str_replace('|post_id|', $r->id, $r->content);
    	}
        return $results;
    }
    Best,
    Ernest Marcinko

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


    #26209
    awaistufail02
    awaistufail02
    Participant

    I have tried it but it is not returning me the numeric post id. Instead it is returning the me this in url

    
    https://pkmobilereviews.com/compare/?post_id=|post_id|
    

    Any other solution?

    #26211
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    I am almost positive that this should work with the custom code. Make sure that the custom code is placed in the correct file. If you want, you can add temporary FTP details, and I can do it for you.

    There is no better solution I’m afraid.

    Best,
    Ernest Marcinko

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


    #26217
    awaistufail02
    awaistufail02
    Participant

    Yes! you were right I was doing something wrong. It is now working.

    Thank you!

    #26219
    awaistufail02
    awaistufail02
    Participant

    Wait..!
    I want to ask one more thing…
    |post_id| is not working in titlefield.
    What’s wrong?

    #26220
    awaistufail02
    awaistufail02
    Participant

    by title field I mean the Advanced Title Field

    #26221
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    It’s because the code only applies on the content. Change that to this, it will work on the title as well:

    add_filter( 'asp_results', 'asp_change_content_post_id', 10, 4 );
    function asp_change_content_post_id($results, $search_id, $is_ajax, $args) {
    	foreach ( $results as $k => &$r ) {
    		$r->content = str_replace('|post_id|', $r->id, $r->content);
    		$r->title = str_replace('|post_id|', $r->id, $r->title);
    	}
        return $results;
    }
    Best,
    Ernest Marcinko

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


    #26224
    awaistufail02
    awaistufail02
    Participant

    Thank you so much!
    I have already rated five stars to your plugin.

    #26226
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    You are very welcome, and thank you for that 🙂

    Best,
    Ernest Marcinko

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


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

You must be logged in to reply to this topic.