This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

Journalt
Journalt

Here is my Search page so far :

use RainLab\Blog\Models\Post;

function onHandleForm(){
    if(post('q'))
    {
        $this['q'] = post('q');

        $keyword_tokens = explode(' ', preg_replace('!\s+!', ' ', $this['q']));

        $posts = Post::isPublished()->with('categories')
        ->where('content', 'LIKE', '%'. $keyword_tokens[0] .'%')
        ->orWhere('title', 'LIKE', '%'. $keyword_tokens[0] .'%')
        ->orWhere('excerpt', 'LIKE', '%'. $keyword_tokens[0] .'%');

        if ( count($keyword_tokens) > 1 ){
            foreach(array_slice($keyword_tokens, 1) as $keyword_token){
                $posts = $posts->orWhere('content', 'LIKE', '%'. $keyword_token .'%')
                ->orWhere('title', 'LIKE', '%'. $keyword_token .'%')
                ->orWhere('excerpt', 'LIKE', '%'. $keyword_token .'%');
            }
        }

        $this['posts'] = $posts->orderBy('published_at', 'DESC')->simplePaginate(10);
        $this->page->meta_title = 'Search '.$this["q"];
    }
}

It might need some improvement (pagination doesn't work)

gabouh
gabouh

Hello Journalt, I been struggling to solve this search funcionality, I implement your code in a page and in the same page am adding html with a form to trigger onHandleForm(), I run some print_r() on this variable $this['posts'] and it prints a big set of arrays and it prints with php code like use namespaces, which I don't know how to give a proper format to the html but I think is the result I want. Could you explain me how you implemented?

f13marhe10651
f13marhe10651

did any one fix this?

Cpt.Meatball
Cpt.Meatball

Personally I think this should be handled by the plugin and not be placed on the page. Proper solution might be to extend the blog plugin and add the functionality there.

1-4 of 4

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.