$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'); ?>php - Search only for posts with specific metadata?|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)

php - Search only for posts with specific metadata?

matteradmin9PV0评论

I have a custom post type which creates events. These events have a start date and a end date. I want to be able to search for the events which occur on the specific day only. So if start date is 2018-10-25 and end date 2018-11-01 it would show up in the search, and if it has start date 2018-10-20 and end date 2018-10-22 it would not show up. Same thing with future events.

How should this be done? The post itself has the start-date and end-date in the metadata. But I don't know how to use this to modify the search form.

At the moment I use the standard search form:

<?php get_search_form(); ?>

I have a custom post type which creates events. These events have a start date and a end date. I want to be able to search for the events which occur on the specific day only. So if start date is 2018-10-25 and end date 2018-11-01 it would show up in the search, and if it has start date 2018-10-20 and end date 2018-10-22 it would not show up. Same thing with future events.

How should this be done? The post itself has the start-date and end-date in the metadata. But I don't know how to use this to modify the search form.

At the moment I use the standard search form:

<?php get_search_form(); ?>
Share Improve this question asked Oct 31, 2018 at 19:56 joq3joq3 3813 silver badges21 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Quick question...which case applies?

  1. If you just need a query for today's date being between two pieces of metadata, I'd create a widget running a fairly straight forward WP_Query and outputting the posts.

  2. If you are asking for the user to input a date to search, I'd probably still use a widget but would use the jQuery UI datepicker method for better user interface and data integrity.

Either way, I wouldn't repurpose the stock search form (which is just a widget anyway).

OK...if it's option 1, try this... build the widget in your functions file.

class my_events_widget extends WP_Widget {
    //custom properties
    public $base_id = 'my_events';
    public $title = 'My Events Title';
    public $description = "Today’s Events";
    //end
    function __construct(){
        parent::__construct($this->base_id,$this->title,['description'=>$this->description]);
    }
    public function widget($args, $instance) {
        $str = '<div class="widget"><h2>'.$this->description.'</h2><ul>';
        $q = new WP_Query([
            'post_type'=>'events',
            'meta_query'=>[
                'relation'=>'AND',
            [
                'key'=>'start_date',
                'value'=>date('Y-m-d'),
                'type'=>'DATE',
                'compare'=>'<='
                ],
            [
                'key'=>'end_date',
                'value'=>date('Y-m-d'),
                'type'=>'DATE',
                'compare'=>'>='
            ]]
        ]);
        if ($posts = $q->get_posts()) {
            foreach ($posts as $post) {
                $str .= sprintf('<li><a href="%s">%s - %s</a></li>', esc_attr(get_permalink($post)), $post->post_title, get_the_date('M j, Y', $post));
            }
        } else $str .= "<li>Nothing posted currently. Please check back.</li>";
        $str .= '</ul></div>';
        echo $str;
    }
}

Now register the widget in your theme's functions file.

function my_custom_widgets() {
  register_widget('my_events_widget');
}
add_action('widgets_init','my_custom_widgets');

You can put the widget in a sidebar or use it in a page/post as a template tag using the_widget() function.

Might need to tweak the WP_Query a little since I couldn't fully test it out but it should get the job done.

Post a comment

comment list (0)

  1. No comments so far