1

I have two Advanced Custom Fields, footnote and date_edited (a datepicker-field). What I want to achieve is to run a wp_query where footnote is not empty. The output I want sorted based on date_edited.

I have tried multiple solutions without being able to solve this.

This is my best try:

$args = array(
    'post_type' => 'page',
    'meta_query' => array(
        'relation'      => 'AND',
        array(
            'key' => 'footnote',
            'value'   => array(''),
            'compare' => 'NOT LIKE'
        ),
        array(
            'key' => 'date_edited',
            'value'   => array(''),
            'type' => 'DATE',   
            'compare' => '='                                        
            'orderby' => 'meta_value_num',
            'order' => 'DESC'
        )
    ),
    'post_status' => 'publish',
    'posts_per_page' => 10,
    'no_found_rows' => true
);

Obviously this is not working. -And it's the date_edited array that is not correct. Does anyone see my errors, and have a correct solution to show?

EDIT after @majick responded

This works. And is a small modification of @majick's suggestion:

$args = array(
    'post_type' => 'page',
    'meta_query' => array(
        array(
            'key' => 'footnote',
            'compare' => 'EXISTS',
        ),
    ),
    'post_status' => 'publish',
    'posts_per_page' => 10,
    'no_found_rows' => true,
    'orderby' => 'meta_value_num',
    'meta_key' => 'date_edited',
    'order' => 'DESC'
);
  • Clean up your code pleas so that it starts on the left, and not right of the middle of the page. – Pieter Goosen Apr 6 '16 at 7:24
  • 1
    How can you even think about comparing a value to an array? the code do not make sense on very basic level – Mark Kaplun Apr 6 '16 at 7:33
  • Done! Sorry for poor formatting. – erolha Apr 6 '16 at 7:33
  • @MarkKaplun You are right. Maybe I shouldn't work on this while being feverish... – erolha Apr 6 '16 at 7:35
  • always better to get well first ;) the orderby and order also seem to be out of place – Mark Kaplun Apr 6 '16 at 7:36
0

You are just confusing the array levels, ordering is not done inside the meta_query level but on the main query level. It is the difference between two classes:

https://codex.wordpress.org/Class_Reference/WP_Query https://codex.wordpress.org/Class_Reference/WP_Meta_Query

Also, as the meta query codex says: "Custom field value can be an array only when compare is 'IN', 'NOT IN', 'BETWEEN', or 'NOT BETWEEN'"... I think you really only need NOT EQUALS here to check footnote value is set... I don't think you need to check if date_edited exists so you have a much simpler query:

$args = array(
    'post_type' => 'page',
    'meta_query' => array(
        'key' => 'footnote',
        'value'   => '',
        'compare' => 'NOT EQUALS'
    ),
    'post_status' => 'publish',
    'posts_per_page' => 10,
    'no_found_rows' => true,
    'orderby' => 'meta_value_num',
    'meta_key' => 'date_edited',
    'order' => 'DESC'
);

Hope this helps... and that your fever has broken? :-)

  • Thank you! Unfortunately, the fever is even stronger. Your suggestion almost works! Please see my updated question for the changes I made, and what works / don't work. The challenge now is the ordering. It orders by footnote no matter what I try. I will respond as soon as I find the correct answer! (Which might take a while...) – erolha Apr 6 '16 at 14:57
  • ah sorry typoed it, that should me meta_key not metakey – majick Apr 6 '16 at 15:02
  • also recommend raw garlic and ginger :-) – majick Apr 6 '16 at 15:04
  • Thank you for the heads up! :) -And you know what? That solved it! (Correcting the typo I mean.) – erolha Apr 6 '16 at 15:07
  • (Unfortunately I don't have enough "reputation" to upvote your answer...) – erolha Apr 6 '16 at 15:11

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.