I am having difficulty getting this function to work. What I have going on is that information about a specific clothing items is created in an "Attire" custom post type. I did this because many classes share similar clothing items so it is easier to create 1 clothing item and then assign it to different classes. The custom fields for attired are stored like this:
[9] => Array
(
[attire-id] => 5680
[cat] => Leotard
[gender] => girl
[assign-classes] => Array
(
[0] => 5576
[1] => 5577
)
[attire-name] => Lilac Skirt Eurotard 10127
)
The 'assign-classes' is storing the IDs of different classes that require that particular attire item. When the class appears on the webpage, I want to do a custom query to only retrieve the clothing items that have the classes ID stored in that 'assign-classes'.
This is the function that is not working.
function get_class_attire($id){
$all_attire = array();
$args = array(
'post_type' => 'attire',
'meta_query'=>array(
array(
'key' => 'assign-classes',
'value' => $id,
'compare' => 'IN'
),
),
'numberposts' => -1,
);
$the_query = get_posts($args);
foreach ( $the_query as $post ) : setup_postdata( $post );
$this_attire = array(
'attire-id' => $post->ID,
'cat' => get_post_meta($post->ID, 'attire-category', true),
'gender' => get_post_meta($post->ID, 'gender', true),
'assign-classes' => get_post_meta($post->ID, 'assign-classes', true),
'attire-name' => get_the_title($post->ID)
);
array_push($all_attire, $this_attire);
endforeach;
wp_reset_postdata();
return $all_attire;
}
I feel like the problem is that when it goes to check, it just sees Array for all of the 'assign-classes' values and then returns nothing. I'm not sure how to get it to look at the values in the array to do the comparison.
I am having difficulty getting this function to work. What I have going on is that information about a specific clothing items is created in an "Attire" custom post type. I did this because many classes share similar clothing items so it is easier to create 1 clothing item and then assign it to different classes. The custom fields for attired are stored like this:
[9] => Array
(
[attire-id] => 5680
[cat] => Leotard
[gender] => girl
[assign-classes] => Array
(
[0] => 5576
[1] => 5577
)
[attire-name] => Lilac Skirt Eurotard 10127
)
The 'assign-classes' is storing the IDs of different classes that require that particular attire item. When the class appears on the webpage, I want to do a custom query to only retrieve the clothing items that have the classes ID stored in that 'assign-classes'.
This is the function that is not working.
function get_class_attire($id){
$all_attire = array();
$args = array(
'post_type' => 'attire',
'meta_query'=>array(
array(
'key' => 'assign-classes',
'value' => $id,
'compare' => 'IN'
),
),
'numberposts' => -1,
);
$the_query = get_posts($args);
foreach ( $the_query as $post ) : setup_postdata( $post );
$this_attire = array(
'attire-id' => $post->ID,
'cat' => get_post_meta($post->ID, 'attire-category', true),
'gender' => get_post_meta($post->ID, 'gender', true),
'assign-classes' => get_post_meta($post->ID, 'assign-classes', true),
'attire-name' => get_the_title($post->ID)
);
array_push($all_attire, $this_attire);
endforeach;
wp_reset_postdata();
return $all_attire;
}
I feel like the problem is that when it goes to check, it just sees Array for all of the 'assign-classes' values and then returns nothing. I'm not sure how to get it to look at the values in the array to do the comparison.
Share Improve this question asked Apr 16, 2020 at 13:00 KevinKevin 275 bronze badges 7 | Show 2 more comments1 Answer
Reset to default 0Thanks to the help of @JacobPeattie the problem was how the data was being saved. So instead of:
if(isset($_POST['assign-classes'])) {
update_post_meta( $post->ID, 'assign-classes', $_POST['assign-classes'] );
}
It is now:
delete_post_meta($post->ID, "assign-classes");
for($i = 0; $i < count($_POST['assign-classes-all']); $i++){
add_post_meta( $post->ID, 'assign-classes', $_POST['assign-classes-all'][$i], false );
}
Only thing that was weird is that at first I tried a foreach
loop which would not iterate more than one time. However, a for
loop would.
add_post_meta()
instead ofupdate_post_meta()
. So my question is do you control the code that is saving the checkboxes, or is a plugin doing it? – Jacob Peattie Commented Apr 16, 2020 at 13:17add_post_meta()
for any boxes that are checked. Is that the gist? – Kevin Commented Apr 16, 2020 at 13:20