如何解决无法以编程方式更新媒体字段
我为每个附件都有一个自定义字段,我正在尝试根据附件内容以编程方式更新此字段。该函数应该检查每个附件字段是否有匹配的关键字,如果有匹配项,它将增加变量 $filter_check
。
这样做的目的是,与其在函数运行时检查每个规则,不如按 $filter_check
的值过滤帖子。例如,如果 $filter_check
等于 1,我知道没有匹配项。如果 $filter_check
大于 1,我知道至少有一个匹配项。
但是我下面的代码不起作用,我看不到任何更新,也不知道我出了什么问题。有人可以帮忙吗?
编辑:不清楚 - 当我调用下面的代码时,它破坏了网站,我收到 wordpress 错误消息。
// Set rules to define field data
$filter_title = array("free","demo","clip") ;
$filter_artist = array("","Test","UnkNown") ;
$filter_album = array("UnkNown","Excluded") ;
$filter_url = array(".m4a",".wav","high-deFinition") ;
$filter_check = 1 ; // Value of 1 means the attachment doesn't meet any rules
// This function searches an array
function strposa($haystack,$needle,$offset=0) {
if(!is_array($needle)) $needle = array($needle);
foreach($needle as $query) {
if(strpos($haystack,$query,$offset) !== false) return true; // stop on first true result
}
return false;
}
// Define which posts to get
$query_post_args = array(
'post_type' => 'attachment','post_mime_type' => 'audio','post_status' => 'inherit','posts_per_page' => - 1,'post_status' => 'public'
) ;
// Create a new query to get an array of matching IDs
$query_audio_list = new WP_Query( $query_post_args );
// then get the matching posts in a loop
$list_post_items = array();
foreach ( $query_audio_list->posts as $id ) {
if ( ! $id ) {
continue; // exit when there are no more attachments to loop through
}
// Extract media details
$media_title = get_the_title($id) ;
$media_artist = wp_get_attachment_Metadata($id,'artist',true) ;
$media_album = wp_get_attachment_Metadata($id,'album',true) ;
$media_url = wp_get_attachment_url($id);
// check if any of the strings include the keywords and increment filter check var if so
if (strposa($media_title,$filter_title) == true) { $filter_check++ } ;
if (strposa($media_artist,$filter_artist) == true) { $filter_check++ } ;
if (strposa($media_album,$filter_album) == true) { $filter_check++ } ;
if (strposa($media_url,$filter_url) == true) { $filter_check++ } ;
// update the attachment with result of filter check variable and echo result
$update['custom_filter'] = $filter_check ;
if (wp_update_attachment_Metadata($id,$update) ) {
echo $id . "has been updated<br>" ; } else {
echo "There was an error updating ID: " . $id . "<br>" ;
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。