最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

meta query - How to filter posts by specific date and its tag

programmeradmin2浏览0评论

I want to show the post dates of posts which is greater than $postdate value. what is the wrong with the following code?

Instead of filtering the data by dates, it shows all the dates. I do not know what the problem is with the logic.

Any help will be most appreciated.

$postdate =  get_the_date('Y-m-d');
$update = new WP_Query(array(
    'posts_per_page' => -1,
    'post_type' => 'post',
    'meta_key' => 'post_date',
    'orderby' => 'meta_value_num',
    'order' => 'ASC' ,
    'meta_query' => array(
        array(
            'key' => 'post_date',
            'compare' => '>',
            'value' => $postdate,
            'type' => 'numeric'
        )
    )    
));

while ($update->have_posts()) {
    $update->the_post(); ?>
        <p class="center">
            <span><a href="<?php the_permalink(); ?>"><?php the_title();  ?></a></span><br>
            <span class="small"><i><?php  the_date();  ?></i></span>
        </p><?php
} ?>

I want to show the post dates of posts which is greater than $postdate value. what is the wrong with the following code?

Instead of filtering the data by dates, it shows all the dates. I do not know what the problem is with the logic.

Any help will be most appreciated.

$postdate =  get_the_date('Y-m-d');
$update = new WP_Query(array(
    'posts_per_page' => -1,
    'post_type' => 'post',
    'meta_key' => 'post_date',
    'orderby' => 'meta_value_num',
    'order' => 'ASC' ,
    'meta_query' => array(
        array(
            'key' => 'post_date',
            'compare' => '>',
            'value' => $postdate,
            'type' => 'numeric'
        )
    )    
));

while ($update->have_posts()) {
    $update->the_post(); ?>
        <p class="center">
            <span><a href="<?php the_permalink(); ?>"><?php the_title();  ?></a></span><br>
            <span class="small"><i><?php  the_date();  ?></i></span>
        </p><?php
} ?>
Share Improve this question edited Mar 25, 2020 at 2:45 WordPress Speed 2,2833 gold badges19 silver badges34 bronze badges asked Jan 4, 2020 at 18:26 AshurAshur 233 silver badges8 bronze badges 7
  • 1 there's a missing quote on the word key that might be generating a syntax error, I don't know that it will get you the posts you want, but without that ' you'll get a screen of death for invalid PHP code – Tom J Nowell Commented Jan 4, 2020 at 19:13
  • Thank you but i added the quote still i canot see any result – Ashur Commented Jan 4, 2020 at 19:17
  • 1 what are the contents of your custom fields 'post_date'? – Michael Commented Jan 4, 2020 at 20:35
  • you can't see any result is interpreted as you don't see the posts, but do you really mean you see nothing at all? Aka a white screen of death? Or the page just stops at the point it reaches your code and nothing comes after it? – Tom J Nowell Commented Jan 4, 2020 at 23:02
  • 1 Also, what's the plugin you mentioned that supports PHP codes for custom HTML? Can you edit your question so the code is how it is locally? The ' is still missing so the code in your question would generate PHP fatal syntax errors, and it breaks the syntax highlighting. Also update the question to explain where and how this code is being ran – Tom J Nowell Commented Jan 4, 2020 at 23:04
 |  Show 2 more comments

1 Answer 1

Reset to default 3

Ashur

Please try following code with date_query. this is working fine at my end. Let me know if you want any additional detail.

<?php
$postdate = get_the_date('Y-m-d');
$args = array(
    'posts_per_page' => -1,
    'post_type' => 'post',
    'date_query' => array(
        'after' => $postdate,
    )
);
$update = new WP_Query($args);

while ($update->have_posts()) {
    $update->the_post();
    $post_id = get_the_ID();
    $post_title = get_the_title();
    $post_date = get_the_date();
    $post_link = get_the_permalink();
    ?>
    <p class="center">
        <span><a href="<?php echo $post_link; ?>"><?php echo $post_title; ?></a></span><br>
        <span class="small"><i><?php echo $post_date; ?></i></span>
    </p>
<?php } ?>
发布评论

评论列表(0)

  1. 暂无评论