Support » Plugin: WPAdverts - Classifieds Plugin » Extrace Search Box

  • Resolved johndoe01

    (@johndoe01)


    Greetings. Again with my site. now i need to place the search box on the header.
    I managed to place it but i have this result:
    https://i.imgur.com/8oTAh2d.png

    And once i so a search i don’t see anything. I placed on the header file the following string

    <?php
    /**
     * Header
     *
     * @package theme
     */
    
    ?>
    <header>
    	<div class="top">
    		<?php get_template_part( 'view/component/logo' ); ?>
    		<a href="javascript:;" class="search-control"><?php the_acf_option( 'label_search' ); ?></a>
    		<a href="javascript:;" class="menu-control"><?php the_acf_option( 'label_menu' ); ?></a>
            
            
    		<?php
    		/*get_template_part( 'view/module/search' );*/
    		/*get_template_part( 'view/component/cta' );*/
            dynamic_sidebar( 'SearchBox' );
            //include ADVERTS_PATH . '/templates/list.php';
            //global $wp_query;
        remove_filter("the_content", "adverts_the_content");
        echo shortcode_adverts_list(array(
            "category" => $wp_query->get_queried_object_id()     ));
            dynamic_sidebar( 'CTA' );
    		?>
    	</div>
    	<div class="nav">        
    		<?php
    		wp_nav_menu(
    			[
    				'menu'        => 'main',
    				'fallback_cb' => null,
    			]
    		);
    		?>
    	</div>
    </header>
    

    I’m not sure what’s needed to be done but what i need first is solve the functionality before customizing the appearance.

    Tried as well to do a custom shortcode on functions but does not work on the header; works in pages but when i use it wirks nut does not show results. Here it is:

    add_shortcode('adverts_search', 'shortcode_adverts_search');
    function shortcode_adverts_search( $atts ) {
    
        wp_enqueue_style( 'adverts-frontend' );
        wp_enqueue_style( 'adverts-icons' );
        wp_enqueue_script( 'adverts-frontend' );
    
        $params = shortcode_atts(array(
            'name' => 'default',
            'author' => null,
            'redirect_to' => '',
            'search_bar' => adverts_config( 'config.ads_list_default__search_bar' ),
            'show_results' => false,
            'category' => null,
            'columns' => adverts_config( 'config.ads_list_default__columns' ),
            'display' => adverts_config( 'config.ads_list_default__display' ),
            'switch_views' => adverts_config( 'config.ads_list_default__switch_views' ),
            'allow_sorting' => 0,
            'order_by' => 'date-desc',
            'paged' => adverts_request("pg", 1),
            'posts_per_page' => adverts_config( 'config.ads_list_default__posts_per_page' ),
            'show_pagination' => true
        ), $atts, 'adverts_list' );
    
        extract( $params );
    
        if( is_numeric( $redirect_to ) ) {
            $action = get_permalink( $redirect_to );
        } else {
            $action = $redirect_to;
        }
        
        $taxonomy = null;
        $meta = array();
        $orderby = array();
        
        $query = adverts_request("query");
        $location = adverts_request("location");
        
        if($location) {
            $meta[] = array('key'=>'adverts_location', 'value'=>$location, 'compare'=>'LIKE');
        }
    
        if( is_string( $category) && $category == "current" && is_tax( "advert_category") ) {
            $category = get_queried_object_id();
        }
        if($category) {
            $taxonomy =  array(
                array(
                    'taxonomy' => 'advert_category',
                    'field'    => 'term_id',
                    'terms'    => $category,
                ),
    	);
        }
    
        if($allow_sorting && adverts_request("adverts_sort")) {
            $adverts_sort = adverts_request("adverts_sort");
        } else {
            $adverts_sort = $order_by;
        }
        
        // options: title, post_date, adverts_price
        $sort_options = apply_filters( "adverts_list_sort_options", array(
            "date" => array(
                "label" => __("Publish Date", "wpadverts"),
                "items" => array(
                    "date-desc" => __("Newest First", "wpadverts"),
                    "date-asc" => __("Oldest First", "wpadverts")
                )
            ),
            "price" => array(
                "label" => __("Price", "wpadverts"),
                "items" => array(
                    "price-asc" => __("Cheapest First", "wpadverts"),
                    "price-desc" => __("Most Expensive First", "wpadverts")
                )
            ),
            "title" => array(
                "label" => __("Title", "wpadverts"),
                "items" => array(
                    "title-asc" => __("From A to Z", "wpadverts"),
                    "title-desc" => __("From Z to A", "wpadverts")
                )
            )
        ) );
        
        $sarr = explode("-", $adverts_sort);
        $sort_current_text = __("Publish Date", "wpadverts");
        $sort_current_title = sprintf( __( "Sort By: %s - %s", "wpadverts"), __("Publish Date", "wpadverts"), __("Newest First", "wpadverts") );
        
        if( isset( $sarr[1] ) && isset( $sort_options[$sarr[0]]["items"][$adverts_sort] ) ) {
    
            $sort_key = $sarr[0];
            $sort_dir = $sarr[1];
    
            if($sort_dir == "asc") {
                $sort_dir = "ASC";
            } else {
                $sort_dir = "DESC";
            }
    
            if($sort_key == "title") {
                $orderby["title"] = $sort_dir;
            } elseif($sort_key == "date") {
                $orderby["date"] = $sort_dir;
            } elseif($sort_key == "price") {
                $orderby["adverts_price__orderby"] = $sort_dir;
                $meta["adverts_price__orderby"] = array(
                    'key' => 'adverts_price',
                    'type' => 'NUMERIC',
                    'compare' => 'NUMERIC',
                );
            } else {
                // apply sorting using adverts_list_query filter.
            }
    
            $sort_current_text = $sort_options[$sort_key]["label"] ;
            $s_descr = $sort_options[$sort_key]["items"][$adverts_sort];
            $sort_current_title = sprintf( __( "Sort By: %s - %s", "wpadverts"), $sort_current_text, $s_descr );
        } else {
            $adverts_sort = $order_by;
            $orderby["date"] = "desc"; 
        }
    
        
        $args = apply_filters( "adverts_list_query", array( 
            'author' => $author,
            'post_type' => 'advert', 
            'post_status' => 'publish',
            'posts_per_page' => $posts_per_page, 
            'paged' => $paged,
            's' => $query,
            'meta_query' => $meta,
            'tax_query' => $taxonomy,
            'orderby' => $orderby
        ), $params);
    
        if( ( $category || isset( $params["tax__advert_category"] ) ) && is_tax( 'advert_category' ) ) {
            $pbase = get_term_link( get_queried_object()->term_id, 'advert_category' );
        } else {
            $pbase = get_the_permalink();
        }
    
        $loop = new WP_Query( $args );
        $paginate_base = apply_filters( 'adverts_list_pagination_base', $pbase . '%_%' );
        $paginate_format = stripos( $paginate_base, '?' ) ? '&pg=%#%' : '?pg=%#%';
    
        include_once ADVERTS_PATH . 'includes/class-html.php';
        include_once ADVERTS_PATH . 'includes/class-form.php';
    
        if( $switch_views && in_array( adverts_request( "display", "" ), array( "grid", "list" ) ) ) {
            $display = adverts_request( "display" );
            add_filter( "adverts_form_load", "adverts_form_search_display_hidden" );
        }
    
        if( $display == "list" ) {
            $columns = 1;
        }
        
        if( adverts_request( "reveal_hidden" ) == "1" ) {
            add_filter( "adverts_form_load", "adverts_form_search_reveal_hidden" );
        }
        
        $form_scheme = apply_filters( "adverts_form_scheme", Adverts::instance()->get("form_search"), $params );
        
        $form = new Adverts_Form( $form_scheme );
        $form->bind( stripslashes_deep( $_GET ) );
        
        $fields_hidden = array();
        $fields_visible = array();
        
        $counter = array(
            "visible-half" => 0,
            "visible-full" => 0,
            "hidden-half" => 0,
            "hidden-full" => 0
        );
        
        foreach($form->get_fields() as $field) {
            
            $search_group = "hidden";
            $search_type = "half";
            
            if( isset( $field['meta']["search_group"] ) ) {
                $search_group = $field['meta']['search_group'];
            }
            
            if( isset( $field['meta']['search_type'] ) ) {
                $search_type = $field['meta']['search_type'];
            }
            
            $counter[ $search_group . '-' . $search_type ]++;
            
            if( $search_type == 'full' ) {
                $field['adverts_list_classes'] = 'advert-input-type-full';
            } else if( $counter[ $search_group . '-' . $search_type ] % 2 == 0 ) {
                $field['adverts_list_classes'] = 'advert-input-type-half advert-input-type-half-right';
            } else {
                $field['adverts_list_classes'] = 'advert-input-type-half advert-input-type-half-left';
            }
            
            if( $search_group == "visible" ) {
                $fields_visible[] = $field;
            } else {
                $fields_hidden[] = $field;
            }
        }
        
        // adverts/templates/list.php
        ob_start();
        include apply_filters( "adverts_template_load", get_template_directory() . '/search.php' );
        return ob_get_clean();
    }
    

    Thanks in advance.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Greg Winiarski

    (@gwin)

    Hi,
    to show just the search bar you should use the shortcode like this

    
    echo shortcode_adverts_list(array(
        "show_results" => 0,
        "redirect_to" => "https://example.com/page/with/adverts_list/results",
        "category" => $wp_query->get_queried_object_id()     
    ));
    

    You can keep the “category” param but in this case, it does not do anything since it is used only when showing results, you will also need to change the “https://example.com/page/with/adverts_list/results&#8221; to actual URL of a page with [adverts_list] which will show the results.

    Note that instead of using the [adverts_list] shortcode you can just use a simple HTML form and style it the way you want, you just need to make sure that the names of the fields in the form will match the names of the fields in [adverts_list] search bar.

    The example form below will do that

    
    <form action="https://example.com/page/with/adverts_list/results" method="get">
    <input type="text" name="query" placeholder="Keyword ..." />
    <input type="text" name="location" placeholder="Location ..." />
    <input type="submit" value="Search" />
    </form>
    

    Indeed.
    Made Both Code blocks and worked as intended.
    Since then was missing to add a category selector, while looking in the forum I found out a GitHub repository, to be exact Yours, and added such code. Now it has the category selector which was missing.

    I kept the custom shortcode and the copy of the list.php as search.php in which i have made the customizations and all elements are set.

    Thanks for the help.

    • This reply was modified 1 month, 2 weeks ago by johndoe01.
    Plugin Author Greg Winiarski

    (@gwin)

    Ok great.

    One note if you have it custom version of the wpadverts/templates/list.php template file then consider creating a child template for this file as explained here https://wpadverts.com/documentation/child-themes-and-templates/ then your changes will not be overwritten on WPAdverts update.

    Yes, that is what i did. I made two copies into the template folder (child), one for the head, and one for the results since i needed the button filters but not the fields.

    If i have questions, i’ll answer here or in other post.

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.