skip to Main Content

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'],

2

Answers


  1. Chosen as BEST ANSWER

    I set my $args like this and it works well ! :

    $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" ?

    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


  2. 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:

    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.

    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. So use this to retrieve your _cp_agenda_group metadata items containing that value.

        '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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search