简体   繁体   English

使用 wordpress 查询对 meta_key 的访问在其他 meta_key 中(使用 cmb2 插件)

[英]Access with wordpress query to meta_key is in other meta_key (with cmb2 plugin)

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);首先我尝试使用( pre_get_posts )创建一个过滤器,但最后我决定为我的自定义帖子(日期)创建一个页面存档;

I take the classic loop of archive page Wordpress and had $args我采用归档页面 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);

My meta_key is call with the plugin CMB2 like this ( 'ID' are meta_key for $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',
    ));
}

if i call directly the field (debut) in my $arg like this :如果我像这样直接调用我的 $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);

My query give me 0 custom posts : date我的查询给了我 0 个自定义帖子:日期

But if i call the meta key : 'meta_key' => '_cp_agenda_group',但是如果我调用元键:'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',我的查询给了我所有的自定义帖子:日期,但我无法控制 'meta_key' => '_cp_agenda_debut',

This is a var dump of the key ('_cp_agenda_group enter code here ' for '_cp_agenda_debut')这是密钥的 var 转储('_cp_agenda_group enter 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 ?用 wordpress 查询?

I've search a lot... but no result.我搜索了很多...但没有结果。

I think my query is correct.我认为我的查询是正确的。 I've test it with simple meta_key.我已经用简单的 meta_key 对其进行了测试。

I always tried to make this without success :我一直试图做到这一点但没有成功:

$ga = '_cp_agenda_group';

'meta_key' => $ga[0]['_cp_agenda_debut'],

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 .在我看来,您想在名为_cp_agenda_group的 postmeta 键中搜索存储在其中的内容的名称,特别是_cp_agenda_debut

Some meta value in WordPress contain simple strings or integers, but other contain more elaborate data structures. WordPress 中的一些元值包含简单的字符串或整数,但其他元值包含更复杂的数据结构。 It looks to me like your _cp_agenda_group values work that way.在我看来,您的_cp_agenda_group值是这样工作的。 (But, I'm not completely confident about that from your question.) (但是,我对你的问题并不完全有信心。)

It looks to me like that data structure is this:在我看来,数据结构是这样的:

array(
  array(
    "_cp_agenda_debut" => 1655875800,
    "_cp_agenda_fin"   => 1654542000,
  ),
);

To store that as meta value in a table WordPress serializes it into a messy text string like this.要将其作为元值存储在表中,WordPress 会将其序列化为这样的混乱文本字符串。

a:1:{i:0;a:2:{s:16:"_cp_agenda_debut";i:1655875800;s:14:"_cp_agenda_fin";i:1654542000;}}

Therefore, when you search your metadata table for _cp_agent_debut you must search the meta value for substrings.因此,当您在元数据表中搜索_cp_agent_debut时,您必须在元值中搜索子字符串。 So use this to retrieve your _cp_agenda_group metadata items containing that value.因此,使用它来检索包含该值的_cp_agenda_group元数据项。

    'meta_key' => '_cp_agenda_group',
    'meta_compare' => 'LIKE',
    'meta_value'   => '"_cp_agent_debut"'

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.请注意,如果您有大量带有该元键的元数据行,这会产生非常慢的查询,这会给您的 MariaDB / MySQL 数据库带来负担。

I set my $args like this and it works well !我这样设置我的 $args 并且效果很好! :

$args = array(
    'post_type' => 'date',
    'order' => 'ASC',
    'orderby' => 'meta_value_num',
    'meta_query' => array(
        array(
            'key' => '_cp_agenda_group_agenda',
            'value' => '"_cp_agenda_debut"',
            'compare' => 'LIKE',
        ),
    ),
);

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" ?我如何在序列化元值中访问字符串的整数:“_cp_agenda_debut”?

a:1:{i:0;a:2:{s:16:"_cp_agenda_debut";i:1655875800;s:14:"_cp_agenda_fin";i:1654542000;}}

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因为,我认为 Wordpress 给了我序列化数据的字符串作为值,但我想要这个字符串的整数,最好订购帖子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM