• I filter custom posts by a custom fields and i would like replace the default post content with the search result.

    class CustomSearch {
    
        public function init() {
            add_action( 'init', array( $this, 'get_search_context' ) );
        }
    
        public function get_search_context( $context ) {
            if ( !isset( $_POST['searchfilter'] ) ) {
                $_POST['searchfilter'] = null;
            }
            $args = array(
                'post_type'   => 'custom_posts',
                'post_status' => 'publish',
                's'           => $_POST['searchfilter'],
                'meta_query'  => array(
                    array(
                        'value'   => $_POST['search_category'],
                        'compare' => 'AND',
                    ),
                ),
            );
    
            // this result should override the default post content
            $context['posts'] = Timber::get_posts( $args );
        }
    }

    Theme index.php

    
    $context = Timber::get_context();
    
    if ( is_singular() ) {
        $context['post'] = Timber::get_post();
    } else {
        $context['posts'] = Timber::get_posts();
    }
    
    .
    .
    .
    
  • The topic ‘Overriding content with search post’ is closed to new replies.