0

I am developing code with wordpress. Because of lacking abilities I am having some trouble.

construction of this questioning part in my wordpress

  • custom-post-type : book // this is my custom-post-type.
  • meta_key : date_all // this is my meta_key of 'book'.
  • meta_value : array('year' => '2012', 'month =>'3', 'date' => '23', 'dw' => '2')

and thers are some book-posts (my custom post type). I am trying to query book-post which is corresponding conditions below:

  • meta_key : date_all
  • meta_value : year = 2018 & month = 3
  • compare : >= or 'after'

I am making some code. but I don't know how to express the 'meta_value'. Because this is array value.

$args = array(
            'post_type' => 'book',
            'posts_per_page' => -1,
            'meta_key' => 'date_all',
            'meta_value'   => ????????,
            'compare' =>  '>=',
        );

$query = new WP_Query($args);
while($query->have_posts() ) : $query->the_post();
the_title(); ?>
endwhile; wp_reset_query();

Can you give some help?

  • 1
    Array is a php data construct, the database doesn't understand this format, it is just a big meaningless string. You can't save data as an array if you want to efficiently query on it. – Milo Dec 13 '17 at 4:07
  • why are you not using a string for the date? – inarilo Dec 13 '17 at 4:44
  • //inarilo using a string? I don't get what you mean exactly. my coding understanding is not good. The data of 'date' is from input field type of date. and I changed the string data into date format using strtotime() function to query with month. – JJang Dec 13 '17 at 5:06
  • //Milo if this is not common coding, can you give me some hint? when you want to get specific date through input field of metabox, how do you coding? this is almost my first coding, so I am lacking of coding. – JJang Dec 13 '17 at 5:11
0

Try this

$args = array(
    'post_type'   => 'book',
    'posts_per_page' => -1,
);

$query = new WP_Query($args);
while($query->have_posts() ) : $query->the_post();     
   $date = get_post_meta( get_the_ID(), 'date_all', true );
   if($date['year'] >= 2018 && $date['month'] == 3){
        the_title();
   }
endwhile; wp_reset_query();
  • Thanks for the reply. I tried as you wrote. but it doesn't work. I don't know why. but every year and month in the loop are changed to 2018 and 3 each. so all the book-posts are shown. – JJang Dec 13 '17 at 8:25
  • sorry i missed an "=" sign for date['month']. I have updated the code. please try again now – Sid Dec 13 '17 at 8:27
  • It works. thank you. Although I haven't you, you must have a beautiful girl friend. I wish you would have good luck to your every work. – JJang Dec 13 '17 at 8:47
  • thanks. it would be really appreciated if you could accept the answer. – Sid Dec 13 '17 at 9:44

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.