ACF fields value not available until saving post manually - wordpress

I have some custom post type "video" and I added some custom ACF fields to it ("video_path", "author_name" and "audio_author"). I'm generating posts in that type programmatically like this:
$video_post_params = array(
'post_title' => wp_strip_all_tags($video_title),
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'video'
);
$video_id = wp_insert_post( $video_post_params );
update_field('video_path', $video_path, $video_id);
update_field('author_name', $video_author, $video_id);
update_field('audio_author', $audio_author, $video_id);
All values are inserted well - when I open the post in back-end everything is fine. However, when I try to use those values, I don't get anything?!?
I'm reading values from template files like this:
get_field('video_path', $video_id)
And if I open the post and just save it without any change everything starts working normally and I'm getting post ACF fields normally after that. Posts created manually, from back-end are working well all the time.
What I'm doing wrong? Do I need some extra step when generating posts from code?
The issue is reported here:
http://support.advancedcustomfields.com/forums/topic/programmatic-post-insertion-acf-fields-and-the-save_post-hook/
But that solution is obviously not working for me - my update_field() functions already are immediately after wp_insert_post().

Found it!
When inserting ACF field value field key must be used. If key name is used instead, as I did, everything is inserted well at first look, but value isn't available until post is saved manually. So it's like:
update_field('field_56e683ab6265f', $video_path, $video_id);
update_field('field_56e68415b5c4b', $video_author, $video_id);
update_field('field_56e6842d58740', $audio_author, $video_id);
What a mess....

If you want to use the field name instead of the field key, you can use add_post_meta
For example:
add_post_meta($video_id, 'video_path', $video_path, true);
add_post_meta($video_id, 'author_name', $video_author, true);
add_post_meta($video_id, 'audio_author', $audio_author, true);

With ACF5 you have to use not post id, but post object, lake that:
update_field('field_56e683ab6265f', $video_path, $video);
update_field('field_56e68415b5c4b', $video_author, $video);
update_field('field_56e6842d58740', $audio_author, $video);

I had the same problem, and I correct it with simply add do_action('acf/save_post', $postID); at the end of the script, and that's all…

Related

Query string in URL makes main query misbehave and pagination not to work in Wordpress

I have archive page of movies in which I am presenting all movies paginated. On side bar I have genres(taxonomy) for movies. When user clicks on one I want results on the page to be filtered according to which genre he clicked.
My way of thinking made me do this using query string in URL. So when user click on genre it requests same URL (archive for movies) but adds ?genre=SOMETHING. Then in pre_get_posts hook I have this if statement to modify main query:
if(
!is_admin() &&
$query->is_main_query() &&
is_post_type_archive('movie') &&
get_query_var('genre')
)
Then after that I have code like this to filter movies by genre user clicked on :
$taxonomyQuery = [
[
'taxonomy' => 'genre',
'field' => 'slug',
'terms' => get_query_var('genre'),
],
];
$query->set('tax_query', $taxonomyQuery);
Sidebar link are constructed like this :
<a href="<?php echo esc_url(add_query_arg('genre', $genre->slug)) ?>">
<?php echo $genre->name; ?>
</a>
Taxonomy is created with name genre so that name is automatically added to query_vars.
When I open archive page of movies /movies/ I get paginated results and everything works fine. But once I click on genre I get this path /movies/?genre=comedy.
pre_get_posts activates and filters movies according to the genre selected but pagination doesnt work. Even if I set $query->set('posts_per_page', 1); I still get more than one result returned from query. Problem only occurs when query string ?genre=SOMETHING gets added to URL and I cannot figure out why.
NOTE: I am relatively new to wordpress development and I do not actually know if this is the right way to do this kind of thing.
Any help is appreciated!
So after some testing with different things I got to this conclusion.
When I registered taxonomy with register_taxonomy I did not place 'query_var' property as I thought it is adding query string value like when you use :
add_filter('query_vars', 'demo_query_vars');
function demo_query_vars($queryVars) {
$queryVars[] = 'genre';
return $queryVars;
}
These two thing are not the same. First one enables you to load taxonomy using query_var. From the docs :
Sets the query var key for this taxonomy. Default `$taxonomy` key. If false, a taxonomy cannot be loaded at `?{query_var}={term_slug}`. If a string, the query `?{query_var}={term_slug}` will be valid.
Second one enables custom query_var and query string to be used, which is what i needed.
FIX: Just disable query_bar 'query_var' => false'
Then add code I posted above to allow query variable to be processed (add_filter function) and you will be able to use your query_var.
NOTE: This problem was caused for me because I my taxonomy is called genre therfore query_var in register_taxonomy function is automatically set to this value. And I also wanted to use the same name for my own custom query_var which made conflicts.
If your taxonomy name is different from your query_var (the query string you wich to use, example if I used ?g=SOMETHING instead of ?genre=SOMETHING) YOU WILL NOT run into this problem since there will be no conflicts between these two variables..
Another possible solution would be to make your custom query_var different than one specified in register_taxonomy if you are using query_var defined in register_taxonomy function. They just need to be different so there are no conflicts.
I am just at the beginning so this potentially could not be right but it for sure has something to do with these 2 variable names being the same.

How to get all featured sellers with dokan

Is it possible to get all featured sellesrs with dokan (wordpress plugin)?
I found only this option, which gets all sellers, but in returned data, there is no mention if the store is featured or not.
$sellers = dokan_get_sellers();
foreach ($sellers['users'] as $seller) {
$store_info = dokan_get_store_info($seller->ID);
}
Next time you want to know exactly what a functions does and how it does whatever it does, just follow the term. Searching for dokan_get_sellers on all files of the plugin takes us to where the function is written and there we see we can pass an array of arguments to it. One of the arguments it accepts is "featured" and it expects a yes or no for value. So, this is what we can do:
$args = array( 'featured' => 'yes' );
$sellers = dokan_get_sellers( $args );
I did not test it, but it should be right.

How to alphabetically sort custom posts if taxonomy is equals to 'custom_taxonomy'?

I'm trying to sort my custom posts alphabetically without touching any of the core files of the plugin.
I've tried the code below in functions.php and it works. BUT I want it to apply only to a certain custom taxonomy and/or post type.
function set_custom_post_types_order($wp_query) {
// 'orderby' value can be any column name
$wp_query->set('orderby', 'title');
// 'order' value can be ASC or DESC
$wp_query->set('order', 'ASC');
}
add_filter('pre_get_posts', 'set_custom_post_types_order');
I've also tried adding filter through it using "get_post_type" but it doesn't sort posts anymore.
note: it goes through the filter and display test var_dump in each post.
you can add conditions to check if its a taxonomy ( http://codex.wordpress.org/Conditional_Tags#A_Taxonomy_Page_.28and_related.29 ) and/or a specific post type ( http://codex.wordpress.org/Function_Reference/get_post_type, use your query object to get the correct ID ).

Wordpress custom field search

I want to filter posts with respect to custom fields added to a post.Now I added two custom fields city,zip for each post. I want to filter posts with respect to these two fields.
How can write a custom query for it.
In the where clause I wrote meta_key='City' and meta_value='myval'. It works and returns the post with custom field City and value 'myval'. But I want to check both City and Zip.How can I do that.
I believe you use meta_query for this - just going through an old project now, looks like meta_query can take in an array of filters:
array( 'posts_per_page' => 10,
'meta_query' => array(
array('key'=>'key', 'value'=>'value', 'compare'=>'='),
array('key'=>'key2', 'value'=>'value2', 'compare'=>'=')
)
)
Obviously completely untested IRL, but looks like it works from my end.

Passing arguments to a Drupal6 View

I'm building an Address Book view based on a simple CCK that I built.
I need to pass the initial letter as argument to the view in order to show only the elements starting with the received letter. How can I do it using Drupal Views? I've searched in arguments but cannot find a way to get substring support but only node/user references or CCK fields.
If you use this:
$view = views_get_view('masvisitados');
$view->execute();
print_r( $view->render() );
It will render with the markup, but If you just want the data (like me), you can try this:
$view = views_get_view('masvisitados');
$view->preview('Mas visitados', 4);
$view = $view->result;
print_r( $view );
It returns an array.
That's exactly what a "glossary view" does. Navigate to /admin/build/views on your site. If the default view called "glossary" is disabled, enable it. You can now edit/inspect/try it to see how it works. The key is in the 'Node: Title' argument, where the "glossary mode" checkbox is selected. For your address book, you can duplicate it and add a filter on the desired node type.
...
$view->set_exposed_input( array('field1' => data1, 'field2' => 'some data') );
...

Resources