I added status because wordpress has statuses like "published, draft, etc"
Wherever your query is for your data provider (default Yii Crud is in the search model so maybe PostSearch in your case) you could change it to something like the following.
public function search($params) {
//to show any post that are active and set to display now (date time) or before now (date time)
$query = Post::find()->where('status=:status AND display_start <=:now', [':status' => 1, ':now' => date("Y-m-d h:m:s")]);
//to show any post that are active and set to display now (date time) or before now (date time) and where a display_end is not greator than or equal to now (date time) uncomment the next line and comment the previous line to use this
//$query = Post::find()->where('status=:status AND display_start <=:now and display_end >=:now', [':status' => 1, ':now' => date("Y-m-d h:m:s")]);
$dataProvider = new ActiveDataProvider([
'query' => $query,
'sort' => [
'defaultOrder' => [
' display_start=> SORT_DESC,
]
]
]);
So now when people view or search the post it will only return results for active post
the above example was for datetime, however, the best is all relative to your data set and use. I would start with datetime and then reevaluate after some usage.
I have the date stored in my table but having trouble implementing the query, this is my current search
public function search($params, $pageSize = 3, $published = false)
{
$query = Article::find();
// this means that editor is trying to see articles
// we will allow him to see published ones and drafts made by him
if ($published === true)
{
$query->where(['status' => Article::STATUS_PUBLISHED]);
$query->orWhere(['user_id' => Yii::$app->user->id]);
}
$dataProvider = new ActiveDataProvider([
'query' => $query,
'sort'=> ['defaultOrder' => ['id' => SORT_DESC]],
'pagination' => [
'pageSize' => $pageSize,
]
]);
if (!($this->load($params) && $this->validate())) {
return $dataProvider;
}
$query->andFilterWhere([
'id' => $this->id,
'user_id' => $this->user_id,
'status' => $this->status,
'category' => $this->category,
]);
$query->andFilterWhere(['like', 'title', $this->title])
->andFilterWhere(['like', 'summary', $this->summary])
->andFilterWhere(['like', 'content', $this->content]);
return $dataProvider;
}