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?

4
  • 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
    Commented Dec 13, 2017 at 4:07
  • why are you not using a string for the date?
    – inarilo
    Commented Dec 13, 2017 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
    Commented Dec 13, 2017 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
    Commented Dec 13, 2017 at 5:11

1 Answer 1

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();
4
  • 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
    Commented Dec 13, 2017 at 8:25
  • sorry i missed an "=" sign for date['month']. I have updated the code. please try again now
    – Sid
    Commented Dec 13, 2017 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
    Commented Dec 13, 2017 at 8:47
  • thanks. it would be really appreciated if you could accept the answer.
    – Sid
    Commented Dec 13, 2017 at 9:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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