-1

So, this is how WordPress is saving my CPT meta:

 array(8) {    
 ["product_id"]=> array(1) { [0]=> string(10) "abcdefg987" }  
 ["end_date"]=> array(1) { [0]=> string(10) "02-02-2015" }  
 } 

But I want it to be like this:

 array(8) {    
 ["product_id"]=>  "abcdefg987" ,
 ["end_date"]=>  "02-02-2015"   
 } 

So, is it possible ?


Here is some of my code:
to save:
update_post_meta($post_id, "end_date", $_POST["end_date"]);
The meta box:

global $post;
$custom = get_post_custom($post->ID);  
$end_date = $custom["end_date"][0]; // <- this was an old hack to this problem  
<p><label>End Date:</label><br />  
<input name="end_date" value="<?php echo $end_date; ?>"></input></p>

What I really want to do ?
What I need is to get the Post (custom) with the 'end_date' greater than today. So, I have a meta_query, but doesn't work, cause also shows me the posts with 'end_date' < today, so I thought is because it get its values in the array, and not just the values, here is the code:

$args = array(
    'post_type' => 'myEventCPT',
    'meta_query' => array(
            array(  'meta_key' => 'end_date',
                    'value' => date("m-d-Y"), // Set today's date 
                    'compare' => '>=', 
                    //'type' => 'DATE'
            )
    )
);  
$my_event_post = get_posts( $args );
  • Your question, your comments and the question update is confusing and about different things. If your true question is what you describe in the last update (getting posts witha meta field greater than today), please use the search in this site, that question has been asked a lot of times. – cybmeta Feb 24 '15 at 15:06
  • @cybmeta, well yeah, I did discover my true problem while looking for the solution of this problem. I will edit/delete my question promptly. Thanks. – Francisco Corrales Morales Feb 24 '15 at 15:16
  • @FranciscoCorralesMorales your date format is wrong, you need to save it in Y-m-d. Think of it as just comparing 8 digit numbers- 02-24-2015 appears earlier than 03-24-1983, because 2242015 < 3241983. – Milo Feb 24 '15 at 17:27
  • @milo, yes you are right !, fixed and now works better, but still, not working properly... – Francisco Corrales Morales Feb 24 '15 at 22:15
1

Your issue is not with storage, it is with retrieval. get_post_custom() is inconvenient and rarely used in practice.

get_post_meta() is more common in practice. Note it has explicit argument to retrieve a single value, rather than array of values for a key (which WP can do too).

| improve this answer | |
  • This is on the right track. The problem, is that it only works, when I set the second argument to a specific key, so if I set it to '', it will just return the array of values. – Francisco Corrales Morales Feb 24 '15 at 14:38
  • Yes, this one is meant to work with specific keys. Which is how it actually happens in practice most of the time, retrieving the complete set of custom fields is uncommon (in my experience). – Rarst Feb 24 '15 at 14:46
  • I see. Well thanks for that !, what I just notice (thanks to you, and other fellows in Reddit), is that my problem is not with that specifically, is with the dates in a meta_query, I have edited my question. – Francisco Corrales Morales Feb 24 '15 at 14:56
0

THe only possible way is this if your html input name is

name="end_date[]"...

SO can you please check it once more

| improve this answer | |
  • Please use the WYSIWYG editor buttons to format your content. – kaiser Feb 24 '15 at 7:53
  • I don't have [] in the name property of the input. – Francisco Corrales Morales Feb 24 '15 at 13:13
  • The problem is with getting the right post data, from a meta_query which involves dates. I have edited my question. – Francisco Corrales Morales Feb 24 '15 at 14:57
  • Solved !, thanks for your help. I posted the solution. – Francisco Corrales Morales Feb 24 '15 at 22:39
0

ok, I fixed (with the help of everybody, from Reddit, and StackExchange), I had several issues.
This is my working code:

$args = array(
    'numberposts' => 1,
    'post_type' => 'my-cpt',
    'order' => 'ASC' ,
    'meta_query' => array(
            'relation' => 'AND',
            array(  'key' => 'end_date',
                    'value' => date("Y-m-d"), //today
                    'compare' => '>=', 
                    'type' => 'DATE'
            ),
            array(  'key' => 'product_id',
                    'value' => $product['id'],
                    'compare' => '=',
                    'type' => 'CHAR'
            ),  
    ),
);  
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.