Insert/Update Listings from Plugin

This topic contains 17 replies, has 3 voices, and was last updated by  Alex Rollin 6 years, 5 months ago.

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

Open Support Ticket
  • Author
    Posts
  • #407149

    Robert Strobel
    Expired Member
    Post count: 54

    Hello,

    We have bought and setup GeoDirectory core plugin and the add-on for Custom Post Types. We use a cpt for all of our directory listings.

    We are writing a plugin that connects to our intranet API and will import data from our intranet into the geodirectory. Can you please point me in the correct direction to be able to:
    – insert new listings with relevant data for fields and categories
    – update existing listings based on a custom field value

    I checked the API docs, and the codex but couldn’t find anything.
    I also checked the add-listing template but that only uses actions and I need a way to call a function that will handle the database interaction naturally so to not cause future issues with updates..

    Many thanks in advance

    #407179

    Stiofan O’Connor
    Site Admin
    Post count: 22956

    Hi Robert,

    I am actually in the middle of rewriting this part, in future it will hook into the standard wp_insert_post() and wp_update_post() functions, you will simply pass the extra fields into the post data.

    If you need to implement this sooner this is the current function:
    https://github.com/GeoDirectory/geodirectory/blob/d1298ab275a7e54b36bb1cd33abacda42df66df2/geodirectory-functions/post_functions.php#L133

    Thanks,

    Stiofan

    #407192

    Robert Strobel
    Expired Member
    Post count: 54

    Thank you Stiofan for that link! Very helpful
    To confirm, geodir_save_listing() can be used for both insert and update right? Would you be able to give a simple example of an update based on custom field?

    Which function would I use to get a listings data? Maybe something like geodir_get_listing?

    Thanks again!

    #407206

    Robert Strobel
    Expired Member
    Post count: 54

    My use case is:
    We have a variable called

    $testvalue = "testval";

    We want to use this $testvalue to update geo directory listings that have a custom field “Test Val” as the same value a this variable $testvalue
    Also, we want to be updating other custom fields within the geodir_save_listing() call

    The goal is to insert a record if it does not already exist with the custom field as $testvalue and if it exists, then update instead

    #407212

    Stiofan O’Connor
    Site Admin
    Post count: 22956

    Yes it is used for insert and update. you can look at out dummy data as an example:
    https://github.com/GeoDirectory/geodirectory/blob/master/geodirectory-admin/dummy-data/standard_places.php

    You can get all post meta info with https://wpgeodirectory.com/codex/codex/geodirectory_functions/geodir_get_post_info/

    For individual fields you can use the GD functions that work the same as the WP ones:
    geodir_save_post_meta
    geodir_get_post_meta
    geodir_delete_post_meta

    Stiofan

    #407221

    Robert Strobel
    Expired Member
    Post count: 54

    Thank you very much.

    Lastly, for custom fields, is there some prefix?

    If i made a custom field called “Test Val” and it’s html variable is test_val then what would the meta key be? Would it be geodir_test_val or would it be test_val?

    #407231

    Alex Rollin
    Moderator
    Post count: 27815

    Hello!

    The prefix is “geodir_”, so, “geodir_test_val”

    thanks

    #407233

    Robert Strobel
    Expired Member
    Post count: 54

    Thanks Alex.

    I tried this simple query to retrieve listings by custom field value. Since I first need the $post_id to use geodir_get_post_info() or _get_post_meta() —

    
    
    $existing = (new WP_Query(array(
    		"meta_query"	=> array(
    			array(
    				"key"	=> "geodir_test_val",
    				"value"	=> "100",
    				// 'compare' => 'IN'
    			)
    		)
    	)))->posts;

    However this returns an empty array, however there is a listing with the custom field value “100” as I created it manually to test with.

    Much thanks for info so far. It has been helpful and insightful.

    #407250

    Alex Rollin
    Moderator
    Post count: 27815

    Hello!

    We will be able to announce a release date for V2 in the near future. As Stiofan mentioned, the new features are currently WIP.

    https://wpgeodirectory.com/support/topic/insertupdate-listings-from-plugin/#post-407179

    #407252

    Robert Strobel
    Expired Member
    Post count: 54

    This is for simply getting the post data nbased on a meta key. That is not even possible?

    I know update/insert what stiofan mentioned.. that’s already covered. But i’m talking about just getting the post data ie post id.

    #407254

    Alex Rollin
    Moderator
    Post count: 27815

    Hello!

    We don’t support programmatic use of the plugin, actually.

    We do support the REST API, if we could persuade you to consider that route.

    I see you are a very talented developer. I will flag this, but, please be patient with us because this is not covered within our support policy.

    Thanks for your patience.

    #407275

    Robert Strobel
    Expired Member
    Post count: 54

    What would the rest api URL be for filtering the listing by custom field?

    #407285

    Alex Rollin
    Moderator
    Post count: 27815
    #407296

    Robert Strobel
    Expired Member
    Post count: 54

    Ok so I installed that and looked at the documentation. Straight forward but I see no examples or references on filtering.

    I tried normal wordpress filtering but it didn’t work

    
    
    $curl = curl_init();
    	$field_string = http_build_query(array(
    		'filter[meta_query]' => array(
    	    	array (
    		        'key' => 'geodir_test_val',
    		        'value' => "1000",
    	    	)
    	    ),
    	));
    	curl_setopt_array($curl, array (
    	    CURLOPT_RETURNTRANSFER => 1,
    	    CURLOPT_URL => site_url('wp-json/geodir/v1/customdir?' . $field_string)
    	  )
    	);
    	$existing = json_decode(curl_exec($curl));

    However the above code brings back all listings and not filtered by field/meta.

    I understand replies take a little bit of time. Are you able to guide me here as I don’t see anything further on the REST API docs.. thanks!

    #407360

    Stiofan O’Connor
    Site Admin
    Post count: 22956

    For filtering you would need to use a custom query at the moment, and join the post table with the place_details table instead of the post_meta table.

    something like:

    ` global $wpdb, $table_prefix, $plugin_prefix,$post;
    $post_id = $post->ID;
    $post_type = $post->post_type;
    $table = $plugin_prefix . $post_type . ‘_detail’;

    $post_arr = $wpdb->get_results($wpdb->prepare(
    “SELECT * FROM $wpdb->posts p JOIN ” . $table . ” gd ON gd.post_id=p.ID WHERE p.ID=%d LIMIT 1″,
    array($post_id)
    ));`

    Stiofan

Viewing 15 posts - 1 through 15 (of 18 total)

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

Open Support Ticket