$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>wp query - Filtering WP_Query based on wp_postmeta keys values|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

wp query - Filtering WP_Query based on wp_postmeta keys values

matteradmin8PV0评论

In the context of a Wordpress widget development, I use WP_Query this way:

$args = array(
    'post_type' => 'myType',
    'post_per_page' => $number,
    'showposts' => $number
);
$latest_myTypes = new WP_Query($args);
if ($latest_myTypes->have_posts()):
    global $post; //etc.

When a myType post is saved, it stores some values in the table wp_postmeta, in particular two keys: day (an integer) and month (a string).

As you can see, in the preceding code I get all the posts of type myType.

I need a different thing: since any single post contains upcoming events, I want to get all the posts where the date is equal or after the current date (so, day must be at least today AND the month should be at least the current month).

Is it possible to modify my WP_Query to filter the posts this way?

I've red about meta_query but I've not understood how to integrate a meta query to my current query, and even how to create the query itself.

In pseudocode, I should get all the posts WHERE:

day >= date("j")

    AND

date("n", strtotime(month)) >= date("n")

Can you help me?

Update: I know it would be much easier if I could store the Unix timestamp of an upcoming event date: problem is I inherited code from another developer and found that he used metaboxes to automatically save data. I couldn't find a way to alter his code to save day, month and year submitted by a user as a combined and unique key value, since he uses three different fields (it would be interesting if I could create a sort of computed field)

Update: I suppose I solved this way:

$args = array(
        'post_type' => 'myType',
        'post_per_page' => $number,
        'showposts' => $number,
        'meta_query' => array(
            array(
                'key'       => 'month',  
                'value'     => array(date("F"),date("F", strtotime('+1 month'))),
                'compare'   => 'IN'
            ),
            array(
                'key'       => 'day',  
                'value'     => date("j"),
                'compare'   => '>='
            ),
            array(
                'key'       => 'year',  
                'value'     => date("Y"),
                'compare'   => '='
            ),
        ),
    );

It is working fine. Any other comment, solution, suggestion, etc. is eventually appreciated.

In the context of a Wordpress widget development, I use WP_Query this way:

$args = array(
    'post_type' => 'myType',
    'post_per_page' => $number,
    'showposts' => $number
);
$latest_myTypes = new WP_Query($args);
if ($latest_myTypes->have_posts()):
    global $post; //etc.

When a myType post is saved, it stores some values in the table wp_postmeta, in particular two keys: day (an integer) and month (a string).

As you can see, in the preceding code I get all the posts of type myType.

I need a different thing: since any single post contains upcoming events, I want to get all the posts where the date is equal or after the current date (so, day must be at least today AND the month should be at least the current month).

Is it possible to modify my WP_Query to filter the posts this way?

I've red about meta_query but I've not understood how to integrate a meta query to my current query, and even how to create the query itself.

In pseudocode, I should get all the posts WHERE:

day >= date("j")

    AND

date("n", strtotime(month)) >= date("n")

Can you help me?

Update: I know it would be much easier if I could store the Unix timestamp of an upcoming event date: problem is I inherited code from another developer and found that he used metaboxes to automatically save data. I couldn't find a way to alter his code to save day, month and year submitted by a user as a combined and unique key value, since he uses three different fields (it would be interesting if I could create a sort of computed field)

Update: I suppose I solved this way:

$args = array(
        'post_type' => 'myType',
        'post_per_page' => $number,
        'showposts' => $number,
        'meta_query' => array(
            array(
                'key'       => 'month',  
                'value'     => array(date("F"),date("F", strtotime('+1 month'))),
                'compare'   => 'IN'
            ),
            array(
                'key'       => 'day',  
                'value'     => date("j"),
                'compare'   => '>='
            ),
            array(
                'key'       => 'year',  
                'value'     => date("Y"),
                'compare'   => '='
            ),
        ),
    );

It is working fine. Any other comment, solution, suggestion, etc. is eventually appreciated.

Share Improve this question edited Nov 11, 2018 at 12:55 asked Nov 10, 2018 at 16:21 user140189user140189
Add a comment  | 

1 Answer 1

Reset to default 0

This is the answer to my question:

$args = array(
        'post_type' => 'myType',
        'post_per_page' => $number,
        'showposts' => $number,
        'meta_query' => array(
            array(
                'key'       => 'month',  
                'value'     => array(date("F"),date("F", strtotime('+1 month'))),
                'compare'   => 'IN'
            ),
            array(
                'key'       => 'day',  
                'value'     => date("j"),
                'compare'   => '>='
            ),
            array(
                'key'       => 'year',  
                'value'     => date("Y"),
                'compare'   => '='
            ),
        ),
    );
Post a comment

comment list (0)

  1. No comments so far