• Resolved Beginner9

    (@beginner9)


    Hi Greg,
    This is the code I have used for adding custom field drop down in search form.
    Still having minor issues.

    add_filter( 'adverts_form_load', 'search_by_custom_fields_form_load' );
    
    function search_by_custom_fields_form_load( $form ) {
    
        if( $form['name'] != 'search' ) {
            return $form;
        }
    
        $form['field'][] = array(
            "name" => "my_custom_fabric",
            "type" => "adverts_field_select",
            "order" => 20,
            "label" => "Fabric",
    		"max_choices" => 4,
    		"options" => array(
    		    array("value"=>"Silk", "text"=>"Silk"),
    		    array("value"=>"Cotton", "text"=>"Cotton"),
                        array("value"=>"Woolen", "text"=>"Woolen"),
    		)
    	);
    
    	add_action( "wp_footer", "search_by_custom_fields_footer" );
    
        return $form;
    }
    
    add_filter( 'adverts_list_query', 'search_by_custom_fields' );
    
    function search_by_custom_fields( $args ) {
      $cz = array("my_custom_fabric"); // <- more fields here
      foreach($cz as $name) {
        if( ! adverts_request( $name ) ) {
          continue;
        }
    
        if( ! isset( $args["meta_query"] ) ) {
          $args["meta_query"] = array();
        }
    
        $args['meta_query'][] = array(
          'key'     => $name,
          'value'   => adverts_request( $name ),
        );
      }
    
      return $args;
    }
    function search_by_custom_fields_footer() {
        ?>
        <style type="text/css">
        .advert-input.advert-input-type-full {
           width: 100% !important;
           margin: 1em 0 0 0 !important;
        }
        </style>
        <?php
    }

    Display of the custom field in the form happens. However,
    – It is treated like hidden (I get the filter icon, beside search icon)
    – The half & full option doesn’t work
    – The Box comes below category box & not beside the category box.
    – The new box is slightly smaller than category search box.
    Also, upon inserting meta – visible/half code, the site crashes.

    Many thanks for your help.
    Regards,

    https://wordpress.org/plugins/wpadverts/

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

    (@gwin)

    Hi,
    note the first code snippet here http://wpadverts.com/documentation/custom-fields-search-form/ you are missing “meta” data

    "meta" => array(
                "search_group" => "visible",
                "search_type" => "full" // or half
            )

    If this code crashes your site what is the error message displayed?

    Thread Starter Beginner9

    (@beginner9)

    Hi Greg,
    This is the code you advised for category search, which works well

    Part 1

    add_filter( 'adverts_form_load', 'search_by_category_form_load' );
    
    function search_by_category_form_load( $form ) {
    
        if( $form['name'] != 'search' ) {
            return $form;
        }
    
        $form['field'][] = array(
            "name" => "ad_category",
            "type" => "adverts_field_select",
            "order" => 20,
            "label" => __("Category", "adverts"),
            "max_choices" => 4,
            "options" => array(),
            "options_callback" => "adverts_taxonomies",
            "meta" => array(
                "search_group" => "visible",
                "search_type" => "half"
            )
        );
    
        add_action( "wp_footer", "search_by_category_footer" );
    
        return $form;
    }

    Part 2

    add_filter( 'adverts_list_query', 'search_by_category_query' );
    
    function search_by_category_query( $args ) {
    
        if( ! adverts_request( "ad_category" ) ) {
            return $args;
        }
    
        $args["tax_query"] = array(
            array(
                'taxonomy' => 'advert_category',
                'field'    => 'term_id',
                'terms'    => adverts_request( "ad_category" ),
            ),
        );
    
        return $args;
    }

    Now on similar lines, this is the code I put for custom field search
    Part 1

    add_filter( 'adverts_form_load', 'search_by_custom_fields_form_load' );
    
    function search_by_custom_fields_form_load( $form ) {
    
        if( $form['name'] != 'search' ) {
            return $form;
        }
    
        $form['field'][] = array(
            "name" => "my_custom_fabric",
            "type" => "adverts_field_select",
            "order" => 20,
            "label" => "Fabric",
    		"max_choices" => 4,
    		"meta" => array(
                "search_group" => "visible",
                "search_type" => "half"
    			)
    		"options" => array(
    		    array("value"=>"Silk", "text"=>"Silk"),
    			array("value"=>"Cotton", "text"=>"Cotton"),
                array("value"=>"Woollen", "text"=>"Woollen"),
    
    			)
    	);
    
    	add_action( "wp_footer", "search_by_custom_fields_footer" );
    
        return $form;
    }

    And code for Part 2 is

    add_filter( 'adverts_list_query', 'search_by_custom_fields' );
    
    function search_by_custom_fields( $args ) {
      $cz = array("my_custom_fabric"); // <- more fields here
      foreach($cz as $name) {
        if( ! adverts_request( $name ) ) {
          continue;
        }
    
        if( ! isset( $args["meta_query"] ) ) {
          $args["meta_query"] = array();
        }
    
        $args['meta_query'][] = array(
          'key'     => $name,
          'value'   => adverts_request( $name ),
        );
      }
    
      return $args;
    }

    The error message I get is

    Parse error: syntax error, unexpected ‘”options”‘ (T_CONSTANT_ENCAPSED_STRING), expecting ‘)’ in C:\xampp\htdocs\ad\wp-content\plugins\wpadverts\includes\functions.php on line 1834

    Probably, the meta is giving some problem. In the category search, there was a callback function and there were no dropdown choices in options array…..

    Many thanks for your help.
    Kind Regards,

    Plugin Author Greg Winiarski

    (@gwin)

    In your 3rd code snippet you are missing “,” before options

    add_filter( 'adverts_form_load', 'search_by_custom_fields_form_load' );
    
    function search_by_custom_fields_form_load( $form ) {
    
        if( $form['name'] != 'search' ) {
            return $form;
        }
    
        $form['field'][] = array(
            "name" => "my_custom_fabric",
            "type" => "adverts_field_select",
            "order" => 20,
            "label" => "Fabric",
    	"max_choices" => 4,
    	"meta" => array(
                "search_group" => "visible",
                "search_type" => "half"
    	),
    	"options" => array(
    	    array("value"=>"Silk", "text"=>"Silk"),
    	    array("value"=>"Cotton", "text"=>"Cotton"),
                array("value"=>"Woollen", "text"=>"Woollen"),
            )
        );
    
        add_action( "wp_footer", "search_by_custom_fields_footer" );
    
        return $form;
    }

    this should be fine.

    Thread Starter Beginner9

    (@beginner9)

    Boss, you are great ! Hats off to you !!
    Many thanks for solving this 🙂

    Warm regards,

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Custom Fields In search Form’ is closed to new replies.