Display custom fields from custom posts in RSS feed

$price = $wpdb->get_row($wpdb->prepare("SELECT salePrice FROM ' . $wpdb->gm_ads_products . ' WHERE post_id = ' . $post->ID . '")); // this seems to be the issue

Yes, that’s right.

  1. First, because $wpdb->prepare() requires at least two parameters, one is the SQL query with placeholders like %d (for numbers) and the other is the replacement values that correspond to the placeholders used in the query.

  2. $wpdb->get_row() by default returns an object, hence you can’t simply use the $price when outputting the salePrice value, i.e. echo $price; is like echo <object>; and that will cause a fatal error in PHP! So you should have used $price->salePrice, e.g. $price->salePrice . '<br/>' and not $price . '<br/>'.

  3. $post is not defined in your function, hence that $post->ID will not going to give you anything and instead, it would even throw a PHP notice saying you’re trying to access a property of (and undefined and) a non-object variable.

  4. The generated SQL query is actually malformed which gives you something like this: SELECT salePrice FROM ' . table_name . ' WHERE post_id = ' . 1 . ' šŸ™ And that’s because you used single quotes (') instead of double quotes (") when concatenating or generating the query.

How to fix the issue

  1. Define the $post variable by adding $post = get_post(); at the top in your function:

    function featuredtoRSS($content) {
    //  global $post; // Yes, this works.
        $post = get_post(); // But I prefer using get_post(). :)
    
        // ... the rest of your code here.
    }
    
  2. Then use this to query the salePrice value:

    $price = $wpdb->get_row( $wpdb->prepare(
        "SELECT salePrice FROM " . $wpdb->gm_ads_products . " WHERE post_id = %d",
        $post->ID // this will replace the placeholder %d above
    ) );
    $price = $price->salePrice;
    

    Alternatively, you can use $wpdb->get_var() which then enables you to do echo $price;, i.e. no need for the $price->salePrice:

    $id = (int) $post->ID;
    $price = $wpdb->get_var(
        "SELECT salePrice FROM " . $wpdb->gm_ads_products . " WHERE post_id = $id"
    );
    

    Note that in the above query, I didn’t use $wpdb->prepare(), but I used the $id variable and I made sure that it’s value is a number.

Also, make sure the $wpdb->gm_ads_products is actually defined and that it contains the correct table name ā€” but it’s not, try using gm_ads_products in place of $wpdb->gm_ads_products.