I'm using NinjaForm plugin on wordpress. Here how to search and retrieve data:
<?php
$args = array(
'form_id' => $form_id,
'user_id' => $user_id,
'fields' => array(
'34' => 'checked',
'54' => 'Hello World',
),
);
// This will return an array of sub objects.
$subs = Ninja_Forms()->subs()->get( $args );
// This is a basic example of how to interact with the returned objects.
// See other documentation for all the methods and properties of the submission object.
foreach ( $subs as $sub ) {
$form_id = $sub->form_id;
$user_id = $sub->user_id;
// Returns an array of [field_id] => [user_value] pairs
$all_fields = $sub->get_all_fields();
// Echoes out the submitted value for a field
echo $sub->get_field( 34 );
}
What I want to do is searching by DateTime fields. How do I do that?
I have tried change args like this but result same.
$args = array(
'form_id' => 5,
'date_modified'=> '2015-07-25 3:19:09'
);
or like this
$args = array(
'form_id' => 5,
'date_modified'=> '< 2015-07-25 3:19:09'
);
Did I do wrong?
Find Ninja DB Table:
Go into your database using phpmyadmin or something and find the table Ninja Forms is using. Hopefully they're using their own table. If not, you can search each wp table for some of the arg data that you know returns a form from Ninja_Forms(). Or go into the Ninja plugin code and try and find where they interact with the db to find which table they write into.
Write your own mysql search code:
Instead of using Ninja's class to search, use wordpress's built in mysql search and throw in the table you found in step 1.
GLOBAL $wpdb;
$wpdb->get_results($wpdb->prepare("SELECT * FROM `ninja_table` WHERE `date_modified` = %s", $strDate));
I haven't tested, but this would be my course of action.
Use begin_date and end_data parameters to get the submissions
$args = array(
'form_id' => $form_id,
'begin_date' => '2015-07-20 0:00:00',
'end_date' => '2015-07-25 3:19:09'
);
$subs = Ninja_Forms()->subs()->get( $args );
Related
I am trying to dynamically populate two dropdown fields in a Gravity Forms form. The first field dynamically populates with the terms available in a custom post type. I want the second dynamically populated field to contain the list of all post titles within the custom post type AND have those titles filtered by the term selected in the previous dropdown. Is it possible to get the value of a dropdown within Gravity Forms and pass that value as a parameter in $args to use the get_posts($args) function?
I started using the following tutorial as a guide. https://docs.gravityforms.com/dynamically-populating-drop-down-fields/
add_filter( 'gform_pre_render_3', 'populate_procedures' );
add_filter( 'gform_pre_validation_3', 'populate_procedures' );
add_filter( 'gform_pre_submission_filter_3', 'populate_procedures' );
add_filter( 'gform_admin_pre_render_3', 'populate_procedures' );
function populate_procedures( $form ) {
// Procedure Category Dropdown
foreach ( $form['fields'] as &$field ) {
The first field. The following code populates a dropdown field containing a list of all of the terms within a custom post type (procedure):
if ( $field->type != 'select' || strpos( $field->cssClass, 'populate_procedure_categories' ) === false ) {
continue;
}
$terms = get_terms( array(
'taxonomy' => 'procedure_category',
'orderby' => 'name',
'order' => 'ASC',
) );
// you can add additional parameters here to alter the posts that are retrieved
// more info: http://codex.wordpress.org/Template_Tags/get_posts
//$posts = get_posts( 'post_type=procedure&numberposts=-1&post_status=publish' );
$choices = array();
foreach ( $terms as $term ) {
$choices[] = array( 'text' => $term->name, 'value' => $term->name );
}
// update 'Select a Post' to whatever you'd like the instructive option to be
$field->placeholder = 'Select Procedure Category';
$field->choices = $choices;
The second field. The following code dynamically populates the field with all of the the post titles of the custom post type (procedure). I want to filter these results based upon the value selected above.
if ( $field->type != 'select' || strpos( $field->cssClass, 'populate_procedures' ) === false ) {
continue;
}
$args = array(
'post_status' => 'publish',
'post_type' => 'procedure',
'procedure_category' => 'cardiovascular',
);
$posts = get_posts( $args );
$choices = array();
foreach ( $posts as $post ) {
$choices[] = array( 'text' => $post->post_title, 'value' => $post->post_title );
}
// update 'Select a Post' to whatever you'd like the instructive option to be
$field->placeholder = 'Select Procedure';
$field->choices = $choices;
}
return $form;
}
The second dynamically populated field successfully pulls in the filtered list of post titles based on the $args if I explicitly listed the term (in the example above I used 'cardiovascular'). What I am wondering is if there is a way to grab the value of the previous field and use that to filter the results of the second field (without having to reload the page). Any ideas? Does Gravity Forms have a functionality like this built in?
Using this method, you would need to use multiple pages and add the second Drop Down field to the second page on the form. Then, when the user submits the first page, you can access the value of the first Drop Down from the $_POST. Gravity Forms has a helper method for doing this called rgpost(). Here's what your $args might look like:
$args = array(
'post_status' => 'publish',
'post_type' => 'procedure',
'procedure_category' => rgpost( 'input_FIELDID' ),
);
Replace FIELDID with the field ID of your first Drop Down.
With that said, if you want to accomplish this without having to touch any code, try Gravity Forms Populate Anything.
https://gravitywiz.com/documentation/gravity-forms-populate-anything/
I need to use custom loop from visual composer:
if( function_exists('vc_map') ) {
vc_map( array(
'base' => 'minimag_popular_post_custom',
'name' => esc_html__( 'Popular Post Custom', "minimag-toolkit" ),
'class' => '',
"category" => esc_html__("Minimag Theme", "minimag-toolkit"),
'params' => array(
array(
// this param
"type" => "loop",
"heading" => esc_html__("Display Custom Loop", "minimag-toolkit"),
"param_name" => "custom_loop",
)
),
) );
}
In past I've used vc_link which had the proper function to retrieve the value in the correct form: vc_build_link($href).
There is some function to extract the data from loop parameter? I've looked in the reference but I've not find nothing.
Here an example of the output that I need to parse:
size:8|order_by:date|order:DESC|post_type:post|categories:32,5|by_id:1537,1673
I need to have something like:
$myVar['size'] = 8;
$myVar['order_by'] = 'date';
$myVar['order'] = 'DESC';
$myVar['post_type'] = 'post';
$myVar['categories'] = array(32,5);
$myVar['by_id'] = array(1537,1673);
tested and working :)
list($args, $wp_query) = vc_build_loop_query($atts["custom_loop"]);
while ( $wp_query->have_posts() ) {
$wp_query->the_post();
}
wp_reset_postdata();
If you know your query I like to create a shotcode in the functions.php of my child-theme to create that. You can pass parameters to create different output and you can use such a shortcode everywhere in your site.
I use wordpress standard with the plugins "Advanced custom fields" and "custom_post_type ui". I created a post_type called deals and added some custom fields with it.
What I need to do now, is filter the results when accessing the rest api like this:
http://localhost:8000/wp-json/wp/v2/deals
Actually I only need the acf part of it. I dont care about the rest.
[{"id":29,"date":"2019-04-12T12:34:14","date_gmt":"2019-04-
12T12:34:14","guid":{"rendered":"http:\/\/localhost:8000\/?
post_type=deals&p=29"},"modified":"2019-04-
12T12:34:14","modified_gmt":"2019-04-12T12:34:14",
"slug":"test-title","status":"publish","type":"deals",
"link":"http:\/\/localhost:8000\/deal s\/test- title\/","template":"",
"meta":[],"tax-deals":[],"acf":{"title":"Title for Deals
Post","description":"","image":false,"date_start":"01.01.1970",
"date_end":"01.01.1970","category":"Kleidung"},"_links":{"self":
[{"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/deals\/29"}],
"collection":[{"href":"http:\/\/localhost:8000\/wp-
json\/wp\/v2\/deals"}],"about":[{"href":"http:\/\/localhost:8000\/wp-
json\/wp\/v2\/types\/deals"}],"wp:attachment":
[{"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/media?
parent=29"}],"wp:term":[{"taxonomy":"tax_deals","embeddable":true,
"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/tax-deals?
post=29"}],"curies":
[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}},
I have already tried using
http://localhost:8000/wp-json/wp/v2/deals?search=id
to get the id or something, but response is empty.
Also this didnt work:
http://localhost:8000/wp-json/wp/v2/deals?id=28
Again empty response.
To summarize: I need to filter my custom post type on my custom fields by the "acf" attribute shown in my response json. How does it work?
EDIT: I already installed "WP REST Filter" but still dont know how to do it.
I suggest you to create a new API where you can customize the output. Take advantage of wordpress function register_rest_route() using this you can create an API from CPT and ACF in one ajax url. And you do not need to install anything.
Check how I get my instructor CPT and mycheckbox ACF.
// your ajaxurl will be: http://localhost/yoursite/wp-json/custom/v2/instructor/
add_action( 'rest_api_init', function () {
register_rest_route( 'custom/v2', '/instructor', array(
'methods' => 'GET',
'callback' => 'instructor_json_query',
));
});
// the callback function
function instructor_json_query(){
// args to get the instructor
$args = array(
'post_type' => 'instructor',
'posts_per_page' => -1,
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'mycheckbox', // your acf key
'compare' => '=',
'value' => '1' // your acf value
)
)
);
$posts = get_posts($args);
// check if $post is empty
if ( empty( $posts ) ) {
return null;
}
// Store data inside $ins_data
$ins_data = array();
$i = 0;
foreach ( $posts as $post ) {
$ins_data[] = array( // you can ad anything here and as many as you want
'id' => $posts[$i]->ID,
'slug' => $posts[$i]->post_name,
'name' => $posts[$i]->post_title,
'imgurl' => get_the_post_thumbnail_url( $posts[$i]->ID, 'medium' ),
);
$i++;
}
// Returned Data
return $ins_data;
}
Then, you can use the link: http://localhost/yoursite/wp-json/custom/v2/instructor/ in your ajax url.
I have here a code below that will insert products into the database in woocommerce :
$post = array(
'post_author' => $user_id,
'post_content' => '',
'post_status' => "publish",
'post_title' => $product->part_num,
'post_parent' => '',
'post_type' => "product",
);
$post_id = wp_insert_post( $post, $wp_error );
But, before inserting a new product, I want to check if a slug is existing in the database, else, I want to add a new one and append a number on the slug.
example input:
$new_url = sanitize_title('This Long Title is what My Post or Page might be');
output :
this-long-title-is-what-my-post-or-page-might-be
Now, I want to check if this slug is already existing in the database. If it is existing, I will append a number on the slug(just like what wordpress permalink is doing). If it is already existing, i want to output this as:
this-long-title-is-what-my-post-or-page-might-be-1
I want to add new product into the database together with this new slug.
Does anybody know?
Not tested but just written that code. Try this and let me know
function slug_exists($slug){
if(get_page_by_path( $slug, OBJECT, 'product' )){
return true;
}
return false;
}
$slug = 'your_slug';
$new_slug = $slug;
$c=1;
while(slug_exists($new_slug)){
$new_slug = $slug.'-'.$c;
$c++;
}
// $new_slug containes your desired slug
I'm using WordPress meta data to register clicks on images, to know which images each user has clicked - and also the total number of clicked images per user. The first part is fine, but I'm struggling to get the counter going, as it's returning a lower amount of meta data than what is actually there.
I have a custom post type gallerier and each gallery has a number of images. I'm using the meta key nedlasting, and I'm identifying each image individually by fetching the url.
Here is how I register clicks, after checking it isn't already:
// Add meta query if it doesnt already exist
function sjekk_nedlasting( $postid, $url, $dato) {
$brukerid = (string)get_current_user_id();
// Check if the image is downloaded previously
$args = array(
'post_type' => 'gallerier',
'meta_query' => array(
array(
'key' => 'nedlasting',
'value' => sprintf(':"%s";', $url),
'compare' => 'LIKE'
),
array(
'key' => 'nedlasting',
'value' => sprintf(':"%s";', $brukerid),
'compare' => 'LIKE'
)
),
'fields' => 'ids'
);
// Perform the query
$nedl_query = new WP_Query( $args );
$nedl_ids = $nedl_query->posts;
// If not already downloaded, register it
if ( empty( $nedl_ids ) ) {
$metaarray = Array(
'user_id' => $brukerid,
'url' => $url,
'date' => $dato
);
add_post_meta( $postid, 'nedlasting', $metaarray );
}
}
Then I'm trying to count those registered clicks using the following function:
// Count number of downloads for a single user
function tell_nedlastinger() {
$brukerid = (string)get_current_user_id();
$args = array(
'post_type' => 'gallerier',
'meta_query' => array(
array(
'key' => 'nedlasting',
'value' => sprintf(':"%s";', $brukerid),
'compare' => 'LIKE'
)
),
'fields' => 'ids'
);
// perform the query
$nedl_query = new WP_Query( $args );
$nedl_ids = $nedl_query->posts;
return count($nedl_ids);
}
The function returns a number, but always much lower than the actual amount of registered meta data/clicks. Anyone seeing a problem?
Edit: I'm pretty sure the problem is that I'm getting the total number of posts, not the total number of meta data entries/clicks - which more often that not is several per post. Any way around that?
I never found a solution to query meta data and get x results per post, so instead of using an array in a single key I split the post meta data into three keys. Then I used $wpdb for a custom query. The final code for the tell_nedlastinger() function:
$brukerid = (string)get_current_user_id();
global $wpdb;
$query = $wpdb->get_results("SELECT * FROM wp_postmeta WHERE (meta_key = 'nedlasting_brukerid' AND meta_value = '$brukerid')");
return count($query);
You can improve the speed of this by using:
$brukerid = (string)get_current_user_id();
global $wpdb;
$count = $wpdb->get_row("SELECT COUNT(*) AS THE_COUNT FROM $wpdb->postmeta WHERE (meta_key = 'nedlasting_brukerid' AND meta_value = '$brukerid')");
return $count->THE_COUNT;
The speed is improved by only having to fetch one row from the database. Also note the use of:
FROM $wpdb->postmeta
This allows for a variable table prefix instead of assuming a table wp_ prefix.
$metaCount = count( get_comment_meta($postId,'key','value') );