admin管理员组文章数量:1306822
I've been struggling with this for a few days and still can't figure it how. What I am trying to do is to add a custom table field into the RSS feed, so I am using Code Snippets for this matter. Below is what I have nw, but believe I have tried all possible combinations that I could find on the WP site.
function featuredtoRSS($content) {
if ( has_post_thumbnail( $post->ID ) && get_post_type() == 'product'){
global $wpdb;
$price = $wpdb->get_row($wpdb->prepare("SELECT salePrice FROM ' . $wpdb->gm_ads_products . ' WHERE post_id = ' . $post->ID . '")); // this seems to be the issue
// $permalink_encoded = urlencode(get_post_permalink( $post->ID ));
$content = '<div>' . get_the_post_thumbnail( $post->ID, 'large', array( 'style' => 'margin-bottom: 15px;' ) ) . '</div><br/>☆☆☆☆☆ ' . wp_trim_words( get_the_title(), 10) . '<br/>' . $price . '<br/>' . wp_trim_words( get_the_content(), 55) . '<br/><a href="' . get_post_permalink( $post->ID ) . '"><button style="max-width:90% !important;display:block;position:relative;margin:0 auto;border: none; background: url(//4.bp.blogspot/-2EO8_Ohre5o/WlxIkEZmQfI/AAAAAAAAjQI/J6vLPjS2qxULn9W-NG9czoA2gfPspeS7gCLcBGAs/s320/get-it-now.jpg) no-repeat center left; padding: auto auto; border-radius:20px;width:320px;height:72px;cursor:pointer;"></button></a>' . $price;
}
return $content;
}
add_filter('the_excerpt_rss', 'featuredtoRSS');
add_filter('the_content_feed', 'featuredtoRSS');
Any ideas? As you have already guessed, it is a shopping site. Not Woo. Everything works as it should in the code above, except for the $price, that does not show in the feed. The field I'd like to be displayed in the feed is salePrice, from the gm_ads_products table. It does display the result when executing a mysql query such as SELECT salePrice FROM gm_ads_products WHERE post_id = 244
I've been struggling with this for a few days and still can't figure it how. What I am trying to do is to add a custom table field into the RSS feed, so I am using Code Snippets for this matter. Below is what I have nw, but believe I have tried all possible combinations that I could find on the WP site.
function featuredtoRSS($content) {
if ( has_post_thumbnail( $post->ID ) && get_post_type() == 'product'){
global $wpdb;
$price = $wpdb->get_row($wpdb->prepare("SELECT salePrice FROM ' . $wpdb->gm_ads_products . ' WHERE post_id = ' . $post->ID . '")); // this seems to be the issue
// $permalink_encoded = urlencode(get_post_permalink( $post->ID ));
$content = '<div>' . get_the_post_thumbnail( $post->ID, 'large', array( 'style' => 'margin-bottom: 15px;' ) ) . '</div><br/>☆☆☆☆☆ ' . wp_trim_words( get_the_title(), 10) . '<br/>' . $price . '<br/>' . wp_trim_words( get_the_content(), 55) . '<br/><a href="' . get_post_permalink( $post->ID ) . '"><button style="max-width:90% !important;display:block;position:relative;margin:0 auto;border: none; background: url(//4.bp.blogspot/-2EO8_Ohre5o/WlxIkEZmQfI/AAAAAAAAjQI/J6vLPjS2qxULn9W-NG9czoA2gfPspeS7gCLcBGAs/s320/get-it-now.jpg) no-repeat center left; padding: auto auto; border-radius:20px;width:320px;height:72px;cursor:pointer;"></button></a>' . $price;
}
return $content;
}
add_filter('the_excerpt_rss', 'featuredtoRSS');
add_filter('the_content_feed', 'featuredtoRSS');
Any ideas? As you have already guessed, it is a shopping site. Not Woo. Everything works as it should in the code above, except for the $price, that does not show in the feed. The field I'd like to be displayed in the feed is salePrice, from the gm_ads_products table. It does display the result when executing a mysql query such as SELECT salePrice FROM gm_ads_products WHERE post_id = 244
Share Improve this question asked Feb 7, 2021 at 14:22 Gadgets MallGadgets Mall 133 bronze badges 01 Answer
Reset to default 0
$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.
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.$wpdb->get_row()
by default returns an object, hence you can't simply use the$price
when outputting thesalePrice
value, i.e.echo $price;
is likeecho <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/>'
.$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.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
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. }
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 doecho $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
.
本文标签: Display custom fields from custom posts in RSS feed
版权声明:本文标题:Display custom fields from custom posts in RSS feed 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741767062a2396525.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论