• Resolved beedaan

    (@beedaan)


    I really like the “Mark as Sold” addon. Over time I imagine my classifieds list will fill up with sold items though. Would it be possible to add an ability to filter out sold items from the adverts_list page?

    • This topic was modified 6 years, 4 months ago by beedaan.
Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author Greg Winiarski

    (@gwin)

    Hi, would you like to have an additional filter in the [adverts_list] search bar which will allow showing only the sold items or would you like to always show not-sold items only?

    If you would like to have an additional filter then this will require some custom programming to add the new input and filter by the “is sold” meta field https://wpadverts.com/documentation/custom-fields-search-form/

    Thread Starter beedaan

    (@beedaan)

    I am working on an additional filter to hide sold items. I have been working through the documentation, but I have gotten a little lost.

    I am trying to filter on the “is_sold” meta field, but I’m not sure if I’m doing this correctly. The snippet is active, but it seems to not be working when I load the [adverts_list] page.

    add_filter( "adverts_form_load", "hide_sold_items_form_load" );
    function hide_sold_items_form_load( $form ) {
        if( $form["name"] != "advert" ) {
            return $form;
        }
    	
        $form["field"][] = array(            
            "name" => "hide_sold_items_radio",
            "type" => "adverts_field_radio",
            "order" => 25,
            "label" => "Hide Sold Items Radio",
            "is_required" => false,
            "validator" => array( ),
            "max_choices" => 2,
            "options" => array( ),
    	"meta" => array (
    	        "is_sold" => "false"
    	)
        );
        
        return $form;
    }
    • This reply was modified 6 years, 2 months ago by beedaan.
    Plugin Author Greg Winiarski

    (@gwin)

    The line if( $form["name"] != "advert" ) { will make sure the code is run only for the [adverts_add] shortcode, i understand you want to have the additional input in the [adverts_list] search bar if so then this line should be if( $form["name"] != "search" ) {

    Thread Starter beedaan

    (@beedaan)

    Thank you for the help and I apologize that I missed the difference between the search bar fields and advert_add custom fields. I am working through the documentation and doing my best to construct the “is sold” meta field filter. I have been looking at the snippets you have on github and trying to learn from example. I think what I need is a meta_query filter, but I’m just not sure how to construct it. How do you filter on the is_sold meta field?

    add_filter( "adverts_form_load", "filter_sold_items_form_load" );
    add_filter( 'adverts_list_query', 'filter_by_sold_query' );
    
    function filter_sold_items_form_load( $form ) {
        if( $form["name"] != "search" ) {
            return $form;
        }
    	
        $form["field"][] = array(            
            "name" => "sold_items_checkbox",
            "type" => "adverts_field_checkbox",
            "order" => 25,
            "label" => "Filter",
            "max_choices" => 1,
    	"options" => array(
        	    array( "value" => 1, "text" => "Hide Sold Items")
    	),
            "meta" => array(
                "search_group" => "visible",
                "search_type" => "half" 
            )
        );
        
        return $form;
    }
    
    function filter_by_sold_query( $args ) {
        
        if( ! adverts_request( "sold_items_checkbox" ) ) {
            return $args;
        }
        
        // TODO Construct correctly
        $args["meta_query"][] = array(
            'key' => 'is_sold'
        );
        
        return $args;
    }
    • This reply was modified 6 years, 2 months ago by beedaan.
    Plugin Author Greg Winiarski

    (@gwin)

    It should be

    
        // TODO Construct correctly
        $args["meta_query"][] = array(
            'key' => Wpadverts_Mas::META
            'value' => '1'
        );
    
    Thread Starter beedaan

    (@beedaan)

    Fantastic! Thank you for your help! I made a slight modification to make sure the “mark as sold” value was not there. Here is my final version for reference

    add_filter( "adverts_form_load", "filter_sold_items_form_load" );
    add_filter( 'adverts_list_query', 'filter_by_sold_query' );
    
    function filter_sold_items_form_load( $form ) {
        if( $form["name"] != "search" ) {
            return $form;
        }
    	
        $form["field"][] = array(            
            "name" => "sold_items_checkbox",
            "type" => "adverts_field_checkbox",
    		"order" => "10",
            "max_choices" => 1,
    	    "options" => array(
        	    array( "value" => 1, "text" => "Hide Sold Items")
    	),
            "meta" => array(
                "search_group" => "visible",
                "search_type" => "half" 
            )
        );
        
        return $form;
    }
    
    function filter_by_sold_query( $args ) {
        
        if( ! adverts_request( "sold_items_checkbox" ) ) {
            return $args;
        }
        
        $args["meta_query"][] = array(
            'key' => Wpadverts_Mas::META,
    	'value' => '1',
    	'compare' => 'NOT EXISTS'
        );
        
        return $args;
    }
    • This reply was modified 6 years, 2 months ago by beedaan.
    Plugin Author Greg Winiarski

    (@gwin)

    Ok great, thanks for sharing.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Filter Out Sold Items from adverts_list’ is closed to new replies.