我正在为电视 wordpress 网站 (www.canal-en-vivo.com) 使用几个 php 函数,它根据 channel 提要是否实时将帖子(即 channel )的状态从已发布更改为草稿或不。我把下面两个功能放在一起,但是好像不行。你们能不能看一下一般的代码结构,如果您发现结构有什么奇怪的地方,请告诉我?
此函数通过检查 URL“$feedurlstr”来确定提要是否有效
// This function determines whether a given post/channel is live or not
// based on whether url $feedurlstr has $string in it.
function LiveOrNot($feedurlstr) {
global $result;
// create curl resource
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, $feedurlstr);
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// $output contains the output string
$output = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
$string = "PP['channel_live'] = true";
if(strstr($output,$string)) {
$result = "live";
} else {
$result = "notlive";
}
return $result;
}
并且此函数运行一个 SQL 查询,该查询根据前一个函数返回 $result 是否为 live 来更改每个帖子的状态。
// Function runs SQL query based on channel status $result
function RunChannelLiveQuery() {
global $key;
global $post_ids;
$key = 'feed';
// This array includes the Post ID to be checked
$post_ids = array(2263, 2249);
foreach ($post_ids as $id) {
// Wordpress function to pull value out of a "custom field" - spits URL
$feedurl = get_post_custom_values($key, $id);
// turns $feedurl into string
$feedurlstr = implode($feedurl);
// Find whether feed is live or not
LiveOrNot($feedurlstr);
// Performs DB Query based on live or not
if ( $result == "live" ) {
mysql_query ("UPDATE 'wp_posts' SET 'post_status' = 'publish' WHERE 'post_id' = '$id'") or die(mysql_error());
}
if ( $result == "notlive" ) {
mysql_query ("UPDATE 'wp_posts' SET 'post_status' = 'draft' WHERE 'post_id' = '$id'") or die(mysql_error());
}
}
}
关于这些函数组可能有什么问题的想法?
最佳答案
虽然这不是必需的,但您可能想要使用 Wordpress API用于更新帖子状态
关于php - 这些功能有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5342175/