php - Search only for posts with specific metadata?

admin2025-06-06  0

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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749213553a317314.html

最新回复(0)