• Hello,

    I have a custom field called “end-date”, and i need to query posts that have not passsed the date i declared to that value.

    for example, i have a post that the meta key end-date is 110302 (year/month/day) and i dont want it in my query because its older than today’s date, but i want the ones that have a value that is still to come, like 110315

    im defining a new query

    $time = time() + ( get_option( 'gmt_offset' ) * 3600 );
    $today = gmdate( 'ymd', $time );
    
    $wp_query  = new WP_Query( array(
    'meta_key' => 'data-final',
    'meta_value' => '$today',
    'meta_compare' => '>=' ) );

    i dont know if its correct, but i want to compare today’s date, to the date i have in the custom field.

    Basicly what i need is, query every post that has a date that is newer than today, ommiting the ones that are older, order those results by another date in another custom field called date.

    Its kind of complex, i hope someone cal help, Thank you

Viewing 12 replies - 1 through 12 (of 12 total)
  • Assuming that the query you showed is working properly, you should be able to use these filters to order the posts by the value of the meta_key = ‘date’. Place this code ahead of your query. Note that the code is UNTESTED.

    function mam_posts_join ($join) {
       global $mam_global_join;
       if ($mam_global_join) $join .= " $mam_global_join";
       return $join;
    }
    function mam_posts_orderby ($orderby) {
       global $mam_global_orderby;
       if ($mam_global_orderby) $orderby = $mam_global_orderby;
       return $orderby;
    }
    add_filter('posts_join','mam_posts_join');
    add_filter('posts_orderby','mam_posts_orderby');
    
    $mam_global_join = "JOIN $wpdb->postmeta mx ON (mx.post_id = $wpdb->posts.ID AND mx.meta_key = 'date')";
    $mam_global_orderby = " mx.meta_value ASC";

    After the query, you should turn off the filters by resetting the two global variables:

    $mam_global_join = $mam_global_orderby = '';

    Thread Starter Jo?o Sardinha

    (@johnsardine)

    Thank you for your answer, but how can i display only the intended posts? Display only the ones that have a date that has not passed.

    Thank you

    Looks like the code you showed should work, but maybe there is a typo in your meta_key: ‘data-final’ vs ‘date-final’?

    Or, are you asking how to code a loop to display the posts?

    Thread Starter Jo?o Sardinha

    (@johnsardine)

    It’s not a typo, im developing the website in Portuguese, i wrote the post in English to be easier ??

    Some help coding the loop would be much apreciated thank you

    You should avoid calling anything $wp_ something because it may interfere with a built-in variable: $wp_query is used by WP, for example.

    $my_query  = new WP_Query( array(
    'meta_key' => 'data-final',
    'meta_value' => '$today',
    'meta_compare' => '>=' ) );

    You can find a great deal of information, including examples in the Codex article The Loop.

    The exact details that you need to code will depend on your theme. I suggest that you look at your index.php and single.php files (if they exist), or loop.php, to see how to code loops for your theme.

    Thread Starter Jo?o Sardinha

    (@johnsardine)

    Thank you ??

    I have this bit, it works, but i need to order the results with another custom field and i dont know how to do that.

    <?php
    $time = time() + ( get_option( 'gmt_offset' ) * 3600 );
    $today = gmdate( 'ymd', $time );
    $wp_query = new WP_Query();
    $wp_query->query('meta_key=data-final&meta_compare=<=&meta_value=' . $today . '&orderby=meta_value&order=desc'.'&paged='.$paged);
    if ($wp_query->have_posts()) : while ($wp_query->have_posts()) : $wp_query->the_post();?>

    i know the function is deprecated in the new version, that why i tried with the array in the first post, can someone help please? Im really lost here

    Use the query you showed at first with the filters that I showed you, followed by the if (have_posts()) loop.

    Thread Starter Jo?o Sardinha

    (@johnsardine)

    I don’t know how to do that, can you help me?

    I also found this, which seems to be very similar or equal to what im looking for https://stackoverflow.com/questions/2427912/wordpress-custom-query

    Trying to solve a problem like this is very difficult when you can only see fragments of the code. Please put your entire template that you have so far in a pastebin and post a link to it here. If at all possible, have it selecting the correct posts even if it is not putting them in the correct order.

    If you do not have a working template, use your index.php instead.

    Thread Starter Jo?o Sardinha

    (@johnsardine)

    https://pastebin.com/XMuEvEYH

    So far its showing, for example, a post with the end date of today, some posts with the end date of yesterday, some tith the date before yesterday, etc… if i switch the sign < to > in the compare it will show me the very first posts in the category im viewing.

    Here is a pastebin of working code: https://pastebin.com/KWr2560F

    Thread Starter Jo?o Sardinha

    (@johnsardine)

    Thank you for your time, i couldnt make that code to work, but a friend helped me and it worked.

    here is the pastebin in case some one need it too

    https://pastebin.com/gkLD5TFJ

    but thank you very much vtxyzzy ??

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Query posts with a certain meta key’ is closed to new replies.