使用WordPress查询到Meta_key的访问在其他Meta_key(带有CMB2插件)中
首先,我尝试使用(pre_get_posts)创建一个过滤器 但是最后,我决定为我的自定义帖子(日期)创建一个页面档案;
我采用了档案页wordpress的经典循环,并用$ args
$args = array(
'post_type' => 'date',
'orderby' => array('meta_value_num' => 'DESC'),
'meta_key' => 'my_metakey_from_CMB2',
'meta_compare' => 'EXISTS',
);
$archive_date_query = new WP_Query($args);
我的meta_key使用了插件cmb2('id'是$ args的meta_key):
add_action('cmb2_admin_init', 'agenda_for_cpt_date');
function agenda_for_cpt_date()
{
$prefix = '_cp_agenda_';
$cmb_cpd = new_cmb2_box(array(
'id' => $prefix . 'properties',
'title' => esc_html__('Propriétés de l\'agenda', 'cmb2'),
'object_types' => array('date'),
'context' => 'after_title',
'priority' => 'high',
));
// Groupes pour les dates si plusieurs dates //
$group_field_id = $cmb_cpd->add_field(array(
'id' => $prefix . 'group',
'type' => 'group',
'repeatable' => true,
'description' => esc_html__('', 'cmb2'),
'options' => array(
'group_title' => esc_html__('Agenda {#}', 'cmb2'), // {#} gets replaced by row number
'add_button' => esc_html__('Ajouter un nouvel agenda', 'cmb2'),
'remove_button' => esc_html__('Supprimer agenda', 'cmb2'),
'sortable' => true, // beta
'closed' => true, // true to have the groups closed by default
),
));
$cmb_cpd->add_group_field($group_field_id, array(
'name' => 'Date et heure de début',
'id' => $prefix . 'debut',
'type' => 'text_datetime_timestamp',
));
$cmb_cpd->add_group_field($group_field_id, array(
'name' => 'Date et heure de fin',
'id' => $prefix . 'fin',
'type' => 'text_datetime_timestamp',
));
}
如果我在我的$ arg中直接调用字段(处女级),则是这样的:
$args = array(
'post_type' => 'date',
'orderby' => array('meta_value_num' => 'DESC'),
'meta_key' => '_cp_agenda_debut',
'meta_compare' => 'EXISTS',
);
$archive_date_query = new WP_Query($args);
我的查询给我0个自定义帖子:日期,
但是如果我调用元密钥:'meta_key'=> '_cp_agenda_group',
我的查询给我所有自定义帖子:日期,但我无法控制'meta_key'=> '_cp_agenda_debut',
这是键的var转储('_cp_agenda_group for'_cp_agenda_debut'在此处输入代码
')
array(1) { [0]=> array(2)
{ ["_cp_agenda_debut"]=> int(1655875800)
["_cp_agenda_fin"]=> int(1654542000)
} }
如果我恢复...是否可能...是否有可能访问在数组中的访问META_KEY? ? 使用WordPress查询?
我已经搜索很多...但是没有结果。
我认为我的查询是正确的。我已经用简单的meta_key进行了测试。
我总是试图在没有成功的情况下做到这一点:
$ga = '_cp_agenda_group';
'meta_key' => $ga[0]['_cp_agenda_debut'],
First i tried to create a filter with ( pre_get_posts )
but finally i decided to create a page archive for my custom post (date);
I take the classic loop of archive page Wordpress and had $args
$args = array(
'post_type' => 'date',
'orderby' => array('meta_value_num' => 'DESC'),
'meta_key' => 'my_metakey_from_CMB2',
'meta_compare' => 'EXISTS',
);
$archive_date_query = new WP_Query($args);
My meta_key is call with the plugin CMB2 like this ( 'ID' are meta_key for $args) :
add_action('cmb2_admin_init', 'agenda_for_cpt_date');
function agenda_for_cpt_date()
{
$prefix = '_cp_agenda_';
$cmb_cpd = new_cmb2_box(array(
'id' => $prefix . 'properties',
'title' => esc_html__('Propriétés de l\'agenda', 'cmb2'),
'object_types' => array('date'),
'context' => 'after_title',
'priority' => 'high',
));
// Groupes pour les dates si plusieurs dates //
$group_field_id = $cmb_cpd->add_field(array(
'id' => $prefix . 'group',
'type' => 'group',
'repeatable' => true,
'description' => esc_html__('', 'cmb2'),
'options' => array(
'group_title' => esc_html__('Agenda {#}', 'cmb2'), // {#} gets replaced by row number
'add_button' => esc_html__('Ajouter un nouvel agenda', 'cmb2'),
'remove_button' => esc_html__('Supprimer agenda', 'cmb2'),
'sortable' => true, // beta
'closed' => true, // true to have the groups closed by default
),
));
$cmb_cpd->add_group_field($group_field_id, array(
'name' => 'Date et heure de début',
'id' => $prefix . 'debut',
'type' => 'text_datetime_timestamp',
));
$cmb_cpd->add_group_field($group_field_id, array(
'name' => 'Date et heure de fin',
'id' => $prefix . 'fin',
'type' => 'text_datetime_timestamp',
));
}
if i call directly the field (debut) in my $arg like this :
$args = array(
'post_type' => 'date',
'orderby' => array('meta_value_num' => 'DESC'),
'meta_key' => '_cp_agenda_debut',
'meta_compare' => 'EXISTS',
);
$archive_date_query = new WP_Query($args);
My query give me 0 custom posts : date
But if i call the meta key : 'meta_key' => '_cp_agenda_group',
My query give me all the custom posts : date but i've no control on 'meta_key' => '_cp_agenda_debut',
This is a var dump of the key ('_cp_agenda_groupenter code here
' for '_cp_agenda_debut')
array(1) { [0]=> array(2)
{ ["_cp_agenda_debut"]=> int(1655875800)
["_cp_agenda_fin"]=> int(1654542000)
} }
If i resume... is it possible to access a meta_key that is in an array ???
with a wordpress query ?
I've search a lot... but no result.
I think my query is correct. I've test it with simple meta_key.
I always tried to make this without success :
$ga = '_cp_agenda_group';
'meta_key' => $ga[0]['_cp_agenda_debut'],
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在我看来,您想搜索名为
_CP_AGENDA_GROUP
中存储在它们中的东西的名称的postmeta键,特别是_CP_AGENDA_DEBUT
。WordPress中的某些元值包含简单的字符串或整数,但另一些包含更复杂的数据结构。在我看来,您的
_CP_AGENDA_GROUP
值以这种方式工作。 (但是,我对您的问题并不完全自信。)在我看来,数据结构就是这样:
将其作为元值存储在表Wordpress 序列化 像这样的文本字符串中。
因此,当您搜索元数据表以查看
_CP_AGENT_DEBUT
时,必须搜索元值中的子字符串。因此,请使用它来检索您的_CP_AGENDA_GROUP
包含该值的元数据项目。然后,您必须检查存储在元值中的数据结构,以获取时间戳。
请注意,如果您使用该元密钥有很多元数据行,这会产生惊人的缓慢查询,从而使您的MariadB / MySQL数据库负担负担。
It looks to me like you want to search your postmeta keys named
_cp_agenda_group
for the name of something stored in them, specifically_cp_agenda_debut
.Some meta value in WordPress contain simple strings or integers, but other contain more elaborate data structures. It looks to me like your
_cp_agenda_group
values work that way. (But, I'm not completely confident about that from your question.)It looks to me like that data structure is this:
To store that as meta value in a table WordPress serializes it into a messy text string like this.
Therefore, when you search your metadata table for
_cp_agent_debut
you must search the meta value for substrings. So use this to retrieve your_cp_agenda_group
metadata items containing that value.Then you'll have to examine the data structure stored in the meta value to get that timestamp.
Notice that this generates astonishingly slow queries that can burden your MariaDB / MySQL database if you have lots of metadata rows with that meta key.
我设置了这样的$ args,它运行良好! :
我所有的自定义帖子(日期)都是礼物。但是以错误的顺序。
在序列化meta_value中,如何访问字符串的整数:“ _cp_agenda_debut”?
因为,我认为WordPress会给我序列化数据的字符串作为值,但我想要该字符串的整数,最好订购帖子
I set my $args like this and it works well ! :
All my custom posts (date) are presents. But in the wrong order.
How in the serialize meta_value can i have access to the integer of the string : "_cp_agenda_debut" ?
Because, i think Wordpress give me the string of serialize datas as value but i want the integer of this string, it's better to order the post