Support Forum
Hi Meta Box,
I'm still a little lost on the best approach when it comes to querying by custom field. I know you recommend we don't querying by custom field but does that still apply when using the plugin FacetWP?
Here is my problem: I've created a custom post type for "Events" and each event can have multiple dates. I'm using the date field in a "Group" so that I can add additional dates. I realize that since it's now in a group that it's serialized.
I'm trying to create an events grid similar to this example (notice the first event has multiple dates associated but only shows up one time): https://share.getcloudapp.com/E0u455kE
And then on the actual "Events" page I would list out each available date like this example: https://share.getcloudapp.com/yAuZ7XGO
That being said what would you advise on best approach to query grid by date field?
Thanks in advance!
Hi Brian,
Query by subfield's value in a group is very difficult because the data saved is a serialized array. I recommend using the cloneable field and field setting clone_as_multiple
to get multiple date and query by its value.
Follow this article for more information https://metabox.io/introducing-clone-as-multiple-feature/
Hi Long,
Thanks for that info as I wasn't aware of the 'clone_as_multiple' option. Will this work if the fields are in a MB Custom Table?
Thanks,
Brian
Hi Brian,
The field setting clone_as_multiple
does not work with the custom table. It saves a serialized array in one cell like the group.
https://docs.metabox.io/extensions/mb-custom-table/#group-fields
Hi Long,
So I just realized that my date fields are within a group and the group is cloneable. So does that mean the 'clone_as_multiple' feature will not work because it's in a group? I have a few other fields that I needed to associate to the date fields that's why I had then in a group.
Thanks,
Brian
Hi,
The setting clone_as_multiple
works with all cloneable fields in the default WordPress table (postmeta). If this setting is set to false, the cloneable groups will be saved in one row like
{serialized array1, serialized array2 ...}
If true, it will be saved in more rows like
{serialized array1}
{serialized array2}
...
And please notice that the group is used for storing data, not for querying.
Hi Logn,
My query & order by custom date field is not working.
Since my date fields are within a group field, and it's the group field that's cloneable what would be the key and value so that my query is ordered by my the custom date field?
My group id is: ct_event_date_group
And my event date field id is: ct_event_date
Here is what I have but it's not working:
// Events Query
$events_query = new WP_Query(array(
'post_type' => 'event',
'posts_per_page' => -1,
'meta_key' => 'ct_event_date_group',
'orderby' => 'meta_value',
'order' => 'DESC'
));
Hi Brian,
As I said, the group is used for storing data (sub-fields value) not for querying by sub-fields value because the value of the group saved in the database is a serialized array. See more in https://metabox.io/create-group-of-custom-fields-with-meta-box-group/#how-does-the-meta-box-group-save-data.
This topic from StackOverFlow might help you to achieve your goal https://stackoverflow.com/questions/26364378/wp-query-when-meta-value-saved-as-serialized-array.
I built a field group to manage books in a catalogue where there are multiple authors for a single book (on frontend will not be shown the authors surnames but only the text 'Ouvrage collectif')
The problem comes when I try to retreive all the authors in alphabetical order (eventually is there a picture too, but the question is secondary!).
The query to clonable fields gives me a blank space...
I tried to set a * in array or a blank space or all the capital letters from A to Z.
My deal is to retreive all the author's surname then match them with names and with uploaded picture file.
The same code - using a single text input - for books with only one author - works fine:
Here the code used for field group and query
$prefix = 'edgr_';
$meta_boxes[] = [
'title' => esc_html__( 'Inserimento Autori vari', 'online-generator' ),
'id' => 'bookauthors',
'post_types' => ['livre'],
'context' => 'after_title',
'autosave' => true,
'fields' => [
[
'type' => 'text',
'name' => esc_html__( 'Cognome', 'online-generator' ),
'id' => $prefix . 'cloned_surname',
'desc' => esc_html__( 'inserire cognome', 'online-generator' ),
'clone' => true,
],
[
'type' => 'text',
'name' => esc_html__( 'Nome', 'online-generator' ),
'id' => $prefix . 'cloned_name',
'desc' => esc_html__( 'inserire nome', 'online-generator' ),
'clone' => true,
],
[
'type' => 'image',
'name' => esc_html__( 'Image', 'online-generator' ),
'id' => $prefix . 'cloned_author_img',
'clone' => true,
],
],
];
-----
$new_args = array(
'post_type' => 'livre',
'meta_query' => array(
array(
'key' => 'edgr_cloned_surname',
'value' => serialize( array( ????? ) ),
'compare' => 'LIKE',
)
),
'posts_per_page' => '-1',
'order' => 'ASC',
'orderby' => array(
'edgr_cloned_surname' => 'ASC',
)
);
?>
<?php $new_query = new WP_Query( $new_args ); ?>
<?php if ($new_query->have_posts()) : while ($new_query->have_posts()) : $new_query->the_post(); ?>
<div class="card">
<?php
$author_c_surname = get_post_meta($post->ID, 'edgr_cloned_name', true);
$author_c_name = get_post_meta($post->ID, 'edgr_cloned_surname', true);
?>
<?php echo '<h5 class="book-author"><a href="' .$link. '">'.$author_c_name.' '. $author_c_surname.'</a></h5>'; ?>
</div>
<?php
endwhile;
endif;
Hi
I updated the query but onle the first author is bivible and the picture is not loaded
<?php
$new_args = array(
'post_type' => 'livre',
'meta_query' => array(
array(
'key' => 'edgr_cloned_surname',
)
),
'posts_per_page' => '-1',
'order' => 'ASC',
'orderby' => array(
'edgr_cloned_surname' => 'ASC',
)
);
?>
<?php $new_query = new WP_Query( $new_args ); ?>
<?php if ($new_query->have_posts()) : while ($new_query->have_posts()) : $new_query->the_post(); ?>
<div class="card">
<?php
$link = get_the_permalink();
$author_c_surname = get_post_meta($post->ID, 'edgr_cloned_surname', true);
$author_c_name = get_post_meta($post->ID, 'edgr_cloned_name', true);
?>
<?php $images_c = get_post_meta($post->ID, 'edgr_cloned_author_img', true);?>
<?php $images_c = rwmb_meta( 'edgr_cloned_author_img', ['limit' => 1] ) ?>
<?php $image = reset( $images_c ) ?>
<img class="bio" src="<?= $image['url']; ?>">
<?php echo '<h5 class="book-author"><a href="' .$link. '">'.$author_c_name.' '.$author_c_surname.'</a></h5>'; ?>
</div>
<?php
endwhile;
endif;
?>
test page is here: