我有一系列的CPT,每个都有自己的自定义元数据库。它们都工作得很好——在应该展示的地方展示,省去了应该怎样做。但我发现的一个问题是,他们也都在每个管理页面上运行他们的查询(下拉菜单等),而且有很多。
我将metabox选项的代码部分包装在条件代码中:
function set_options() {
parent::set_options();
global $pagenow;
if ( \'post-new.php\' == $pagenow || \'post.php\' == $pagenow ) {
$notes_query = new WP_Query(array(
\'post_type\' => \'session_note\',
\'posts_per_page\' => -1, // show all
\'orderby\' => \'title\',
\'order\' => \'DESC\',
\'no_found_rows\' => true,
\'cache_results\' => false,
));
$notes_options = array();
foreach ($notes_query->posts as $note) {
$notes_options[$note->ID] = get_the_title($note->ID);
}
$speaker_query = new WP_Query(array(
\'post_type\' => \'speaker\',
\'posts_per_page\' => -1, // show all
\'orderby\' => \'title\',
\'order\' => \'ASC\',
\'no_found_rows\' => true,
\'cache_results\' => false,
));
$speaker_options = array();
foreach ($speaker_query->posts as $speaker) {
$company = get_post_meta($speaker->ID, \'_conferencer_company\', true);
$speaker_options[$speaker->ID] = get_the_title($speaker->ID).\' – \'.get_the_title($company);
}
$this->options = array_merge($this->options, array(
\'note\' => array(
\'type\' => \'multi-select\',
\'label\' => "Special Note",
\'options\' => $notes_options,),
\'speakers\' => array(
\'type\' => \'multi-select\',
\'label\' => "Presenters",
\'options\' => $speaker_options,),
));
}}
。。。这将代码限制为仅在添加或编辑帖子时使用,从而节省了其他管理员大量不必要的查询时间。所有元框都会加载fine和保存fine,但无论帖子类型如何,所有查询仍会在所有编辑/添加帖子页面上运行。
我尝试将查询限制为特定的帖子类型:
function set_options() {
parent::set_options();
global $pagenow;
if ( isset( $_GET[\'post_type\'] )
&& $_GET[\'post_type\'] == \'session\'
&& \'post-new.php\' == $pagenow || isset( $_GET[\'post\'] )
&& \'session\' == get_post_type( $_GET[\'post\'] )
&& \'post.php\' == $pagenow ) {
// do your metabox options thing - same code as above
}}
(“session”是CPT之一)
现在,问题是。。。根据需要,特定帖子类型的元框仍会显示在编辑/添加帖子上,对元框选项的查询也只会运行特定帖子类型的页面-太棒了-除了。。。更新页面时不会保存metabox字段!
我错过了什么?我需要另一个吗$pagenow
对于更新过程?如果是,是什么?
编辑
作为示例,在上面的条件代码中添加了一个选项查询。正在将post/meta功能保存到以下位置:
function save_post($post_id) {
if (get_post_type($post_id) != $this->slug) return;
if (defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE) return;
if (!wp_verify_nonce($_POST[\'conferencer_nonce\'], plugin_basename(__FILE__))) return;
if (!current_user_can(\'edit_post\', $post_id)) return;
foreach($this->options as $key => $option) {
if ($option[\'type\'] == \'internal\') continue;
$value = deep_trim($_POST[\'conferencer_\'.$key]);
if ($option[\'type\'] == \'int\') $value = intval($value);
if ($option[\'type\'] == \'money\') $value = floatVal($value);
if ($option[\'type\'] == \'multi-select\') {
$values = array();
foreach ($_POST[\'conferencer_\'.$key] as $value) { if (!empty($value)) $values[] = $value; }
$value = $values;
}
if ($option[\'type\'] == \'date-time\') {
$date = getdate(strtotime($_POST[\'conferencer_\'.$key][\'date\']));
$time = getdate(strtotime($_POST[\'conferencer_\'.$key][\'time\']));
$value = mktime($time[\'hours\'],$time[\'minutes\'],$time[\'seconds\'],$date[\'mon\'],$date[\'mday\'],$date[\'year\']);
}
update_post_meta($post_id, \'_conferencer_\'.$key, $value);
}
}
希望这能提供更多的见解。