Use action or filter to modify listing before save

This topic contains 4 replies, has 3 voices, and was last updated by  Stiofan O’Connor 7 years, 3 months ago.

We have moved to a support ticketing system and our forums are now closed.

Open Support Ticket
  • Author
    Posts
  • #347702

    Adam
    Expired Member
    Post count: 28

    I’m trying to modify my GeoDirectory listings before they are saved to the database, both upon creation and for updates. Here’s a very simple plugin that demonstrates the error I am experiencing. When I save a listing, I would expect the tag “Test” to be saved with the listing, but nothing happens. Here’s where the filter is applied in source.

    
    
    <?php if ( !defined('ABSPATH') ) exit();
    
    /*
    Plugin Name: GeoDirectory DTEC Overrides2
    Version: 1.0.0
    */
    
    add_filter( 'geodir_listinginfo_request', 'gd_override_tags' );
    
    function gd_override_tags( $listing_info ) {
    	
    	// var_dump($listing_info);
    	
    	$listing_info['post_tags'] = 'Test';
    	
    	// var_dump($listing_info);
    	// die();
    
    	return $listing_info;
    }
    #347714

    Kiran
    Moderator
    Post count: 7069

    Hello Adam,

    ‘geodir_listinginfo_request’ filters the data that need be saved in listing detail table. See desc https://github.com/GeoDirectory/geodirectory/blob/master/geodirectory-functions/post_functions.php#L601

    You code shows that you want to filter tags data that saved in detail table. But tags are also a term taxonomy of the listing, so you have to set tags in term taxonomy as well.

    Check following code snippet

    
    
    function gd_override_tags( $listing_info, $post_id ) {
        if ( !isset( $listing_info['post_tags'] ) ) {
            return $listing_info;
        }
        
        $post_tags_array = array( 'my tag', 'my another tag' ); // Your tags here.
        
        $listing_info['post_tags'] = implode( ",", $post_tags_array );
    
        if ($post_id > 0) {
            $tag_taxonomy = get_post_type( $post_id ) . '_tags';
            
            // Set listing tag terms
            wp_set_object_terms( $post_id, $post_tags_array, $tag_taxonomy ); // Ex: $post_tags_array => array('test', 'mytest'), $tag_taxonomy => 'gd_place_tags'
        }
        
        return $listing_info;
    }
    add_filter( 'geodir_listinginfo_request', 'gd_override_tags', 10, 2 );

    Thanks,
    Kiran

    #347880

    Adam
    Expired Member
    Post count: 28

    Thanks. Similarly, how can I edit the selected categories? I don’t see categories in the listing info object.

    #347934

    Adam
    Expired Member
    Post count: 28

    I guess I could use wp_get_post_categories() and wp_set_post_categories(). It just seems odd that they’re not included with the listing info.

    #348015

    Stiofan O’Connor
    Site Admin
    Post count: 22956

    Categories are also a taxonomy, you would set them like tags but you would use this taxonomy

    $cat_taxonomy = get_post_type( $post_id ) . 'category';

    (yes no underscore prefix like tags)

    Thanks,

    Stiofan

Viewing 5 posts - 1 through 5 (of 5 total)

We have moved to a support ticketing system and our forums are now closed.

Open Support Ticket