I feel like I’m really close to a solution, but yet so far away! The function is working when the ‘import_id’ doesn’t exist, but it goes terribly (loopy) wrong when the ‘import_id’ does exists, and only needs to update the ‘last_updated’ field. Can anyone please help me with this?
function shop_update() {
//test ID
$import_id = '2';
$key = 'field_54c0cd2019a8a'; //field key for 'shops' repeater
$date = date( 'F j, Y g:i a', current_time('timestamp') );
$post_id = 'option';
$exists = false;
$shops = get_field($key, $post_id);
if( have_rows($key, 'option') ) {
//check if import_id exists in current Shops
while (have_rows($key, 'option')) {
the_row();
if( get_sub_field('import_id') == $import_id )
{
$exists = true;
}
}
}
//edit or add repeater row if item does or doesn't exist
if( $exists == false )
{
//add the import_id in total
$shops[] = array('import_id' => $import_id, 'last_update' => $date );
update_field( $key, $shops, $post_id );
}
else
{
if( have_rows('field_54c0cd2019a8a', 'option') ) {
//update 'last_update' subfield
while (have_rows('field_54c0cd2019a8a', 'option')) {
the_row();
if( get_sub_field('import_id') == $import_id )
{
$shops[] = array("import_id" => $import_id, "last_update" => $date );
}
}
}
update_field( $key, $shops, $post_id );
}
}
Here is what I have so far. I’m able to see all the clients from my custom post type and all the posts attached to clients. However, I want it to only show the posts relating to a particular client. I’m not sure what I’m missing.
The ‘attach_to_client’ is the relationship function to the custom post type of ‘clients’.
<?php
$loop = new WP_Query( array( 'post_type' => 'clients', 'posts_per_page' => 10 ) );
$ids = get_field('attach_to_client', false, false);
$query = new WP_Query(array(
'post_type' => array('research', 'project_brief','persona','task_models', 'user_stories', 'style_guide'),
'posts_per_page' => -1,
'post__in' => $ids,
'post_status' => 'any',
'orderby' => 'modified',
));
?>
<?php if($loop->have_posts()) : ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="postbox-container postbox">
<h3><?php the_title(); ?></h3>
<?php if ($query->have_posts()) : ?>
<ul>
<?php
while($query->have_posts()) :
$query->the_post();
?>
<li><?php the_title(); ?></li>
<?php endwhile; ?>
</ul>
<?php else: ?>
<p>Oops, there are no posts.</p>
<?php endif; ?>
</div>
<?php endwhile; endif; wp_reset_query(); ?>
cinq75,
Have a look at my post about $wpdb. This might help you as well. Do something like this:
First, get all post ids of bands for a particular venue id, like:
global $wpdb;
$bands = $wpdb->get_results( "SELECT * FROM wp_postmeta WHERE meta_key LIKE 'venuerepeaterfieldname_%_band' AND meta_value = '{$venue_id}'", OBJECT );
The $bands object will contain the band post ids and also the row number which is the order in which it lies in the repeater field . The repeater row index is important information because you can use that to get all other fields in the same row of that repeater field.
For example, if you band added to the venue as the 3rd row of the repeater field, you will have that information in the $bands object array as “venuerepeaterfieldname_3_band”. You can then do something like:
$band_index_array = explode("_","venuerepeaterfieldname_3_band");
$band_index = $row_index_array[1]; //equal to 3
Then simply get all the values on that repeater row, using:
$bands = get_field('venuerepeaterfieldname', $venue_id);
$concerned_band = $bands[$band_index];
$concerned_band array will contain all fields that you may need. Hope that helps.
Ok,
I was able to find a solution. Its what Elliot has suggested but using $wpdb. For some reason, Elliot’s meta query example did not work for me (returns no results).
Here is what I did:
global $wpdb;
$meetings = $wpdb->get_results( "SELECT post_id FROM wp_postmeta WHERE meta_key LIKE 'members_%_staff' AND meta_value = '{$user_id}'", OBJECT );
In the above example:
$meetings contains all the post ids that have the staff (user) added as a subfield.
Like Elliot mentioned, the “%” is important as we don’t know the row index of the subfield.
$wpdb packs so much power than your standard wordpress $args query model. Hope that helps someone π
Couple things:
get_field
in this manner you may need to pass two additional parameters according to the docs at http://www.advancedcustomfields.com/resources/relationship/post_type => 'any'
fails, so might be worth a shot with a set array just to test
<?php
$ids = get_field('attach_to_client', false, false);
$query = new WP_Query(array(
'post_type' => array('post','page'),
'posts_per_page' => 10,
'post__in' => $ids,
'post_status' => 'any',
'orderby' => 'modified',
));
?>
<h3><?php get_field( 'clients' ) ?></h3>
<?php if ($query->have_posts()) : ?>
<ul>
<?php
while($query->have_posts()) :
$query->the_post();
?>
<li><?php the_title(); ?></li>
<?php endwhile; ?>
</ul>
<?php else: ?>
<p>Oops, there are no posts.</p>
<?php
endif;
?>
thanks it work fine π quick and great support
Interesting, your new template shouldn’t need to be registered – if it’s loading it should be set.
Have you tried your code within the init hook like @elliot mentioned in that post?
add_action('init', function() {
?>
<?php if(get_field('video_slider', 'option')): ?>
<?php while(has_sub_field('video_slider', 'option')): ?>
<?php $image = get_sub_field('image');?>
<li class="home-lead-image">
<a href="<?php the_sub_field('link'); ?>">
<img class="img-responsive upsize" style="visibility: hidden;" src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>"/>
<img class="img-responsive parallax upsize" src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>"/>
<div class="container-fluid">
<div class="col-sm-6">
<h1><?php the_sub_field('text'); ?></h1>
</div>
</div>
</a>
</li>
<?php endwhile; ?>
<?php endif; ?>
<?php
});
Ah, maybe like:
<?php if (in_array("Administration", (array) get_field('dir-department')) || ('yes' == get_field('dir-department-head'))): ?>
Could do something like:
/*
* Create a new post and add field data
* - if the post does not already contain a "reference" to the field object, you must use the field_key instead of the field_name.
*/
// Create post object
$my_post = array(
'post_title' => 'My post',
'post_content' => 'This is my post.',
'post_status' => 'publish',
'post_author' => 1
);
// Insert the post into the database
$post_id = wp_insert_post( $my_post );
// Add field value
update_field( "field_5039a99716d1d", "I am a value!", $post_id );
Snippet from: http://www.advancedcustomfields.com/resources/update_field/
Thanks! I have thought about doing 2 queries but I’m trying to salvage the setup of the their template if possible.
Right now I’ve got a query like this:
<?php
$args = array(
'post_type' => 'staff',
'posts_per_page' => -1,
'meta_key' => 'dir-last-name',
'orderby' => 'meta_value',
'order' => 'ASC'
);
query_posts($args);
?>
So that is grabbing everyone’s last name and sorting it. The template also has this bit of code, which I’m hoping to modify:
<?php if (in_array("Administration", (array) get_field('dir-department'))): ?>
In this bit, it’s looking for entries in the Administration department. Code below that (not included here) formats the entry nicely.
What I would love to do is modify this to also look for a value of “yes” in the ‘dir-department-head’ field. So I want to look for someone in the Administration department (like the code above) and then also check to see if they are a Department Head. Basically an if/else statement that checks for a Department Head, then moves on to the rest of the list.
I know only enough PHP to be dangerous so everything I’ve tried to do doesn’t seem to work π
hey jenkma02
How did you get on with this? I have a similar requirement. I need to be able to add custom fields to the buddypress groups
Hi @pagol,
One way to achieve this would be: First, have two pages, one to display the Choices/tabs and the other one to display the posts for a particular chioice/tab.
On the template for the page that displays the posts, add the following code:
$value = $_GET['value']; // get value of selected choice.
$args = array(
...
'meta_key' => '<select_field_name>',
'meta_value' => $value
);
$the_query = new WP_Query( $args );
... // display posts
Where $value will be the value of the selected choice/tab passed through a query string e.g. The CK tab could open this link http://your-domain.com/page-that-displays-posts/?value=CK
For more info on this, have a look at the following resources:
Filter posts by custom fields,
Creating a WP archive with custom field filter
After some searching I found this post: http://support.advancedcustomfields.com/forums/topic/repeater-on-options-page-returns-count/
Which doesn’t directly apply to me as the code isn’t in functions.php, but it does imply that the repeater field “is not yet loaded and canβt run itβs custom load_value logic”.
I made the category page template in question (category-music.php) after installing ACF and creating the fields, do I have to register the new templates somehow?
If I use acf_form
to edit an existing post, the nonce validation doesn’t pass on the first submit, but works fine on subsequent submits. I lose all my changes and it returns to the edit form (acf_form
). Does the nonce get calculated with a referer that needs to be the same as the page where acf_form
is displayed?
EDIT:
I’m also running WP on Apache behind Nginx, which acts as a reverse proxy. Client IPs and such are forwarded correctly.
Hi @maxis,
Well, to get ACF values associated with a particular term, you have to pass an extra argument to the_field(...)
or get_field(...)
method to target the term ID. This argument can either be a term object or a term string as explained in the docs. The following code should work for you:
foreach($categories as $category):
...
$image = get_image('image_field_name', $category);
echo '<img src="'.$image['url'].'" alt="'.$image['alt'].'" />';
endforeach;
Thanks for the reply, I updated the syntax, still works on homepage.php but not category.php
Weirdly when I dump the video_slider field on the category page I get this:
string(1) "2"
the “2” bit seems to be a count of the array, when there are three rows the dump updates to:
string(1) "3"
When I run the same dump code on homepage.php it returns the data correctly.
I seem to be able to get other standard text option fields on category.php, maybe it’s a combination of it being an option and a repeater field?
Hi @Ellimist,
One way to achieve this would be through the use of acf/fields/post_object/query
filter to modify the $args
array which is used to query the posts shown in the post_object select field. The code should as shown below:
function my_post_object_query( $args, $field, $post ) {
$args['author'] = get_current_user_id()
return $args;
}
add_filter('acf/fields/post_object/query/name=my_post_object', 'my_post_object_query', 10, 3);
For more information, have a look at: acf/fields/post_object/query
Hi @GnuandYou,
Have you tried using the acf/load_field
filter to alter the choices for the radio button? Its pretty simple, try this:
add_filter('acf/load_field/name=my_radio)button', 'my_acf_load_field');
function my_acf_load_field( $field ) {
$field['choices'] = array(
'image_1' => '<img src=".../image_1.jpg" alt="image" />',
'image_2' => '<img src=".../image_2.jpg" alt="image" />'
...
);
return $field;
}
For more information about this, have a look at: acf/load_field
Interesting it’s working on the homepage, the syntax seems like it would need to be tweaked to:
<!-- Start homepage slider -->
<div class="home-banner flexslider-home">
<ul class="slides">
<?php if (have_rows('video_slider', 'option')): ?>
<?php while(have_rows('video_slider', 'option')): the_row(); ?>
<?php $image = get_sub_field('image');?>
<li class="home-lead-image">
<a href="<?php the_sub_field('link'); ?>">
<img class="img-responsive upsize" style="visibility: hidden;" src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>"/>
<img class="img-responsive parallax upsize" src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>"/>
<div class="container-fluid">
<div class="col-sm-6">
<h1><?php the_sub_field('text'); ?></h1>
</div>
</div>
</a>
</li>
<?php endwhile; ?>
<?php endif; ?>
</ul>
</div>
<div class="container-fluid">
<div class="banner-direction-nav">
<a class="prev">β</a>
<a class="next">βΆ</a>
</div>
</div>
<!-- End homepage slider -->
Yes, that’s what I’m seeing. I put in a console.log, see if this helps:
input.min.js?ver=5.1.7:587 s.replace:
2015-01-22 22:56:56.556input.min.js?ver=5.1.7:588 function replace() { [native code] }
2015-01-22 22:56:56.556input.min.js?ver=5.1.7:585 s:
2015-01-22 22:56:56.556input.min.js?ver=5.1.7:586 flexible_content
2015-01-22 22:56:56.556input.min.js?ver=5.1.7:587 s.replace:
2015-01-22 22:56:56.557input.min.js?ver=5.1.7:588 function replace() { [native code] }
2015-01-22 22:56:56.557input.min.js?ver=5.1.7:585 s:
2015-01-22 22:56:56.557input.min.js?ver=5.1.7:586 gallery
2015-01-22 22:56:56.557input.min.js?ver=5.1.7:587 s.replace:
2015-01-22 22:56:56.557input.min.js?ver=5.1.7:588 function replace() { [native code] }
input.min.js?ver=5.1.7:585 s:
2015-01-22 22:56:57.004input.min.js?ver=5.1.7:586 Object {type: "star_rating"}
2015-01-22 22:56:57.006input.min.js?ver=5.1.7:587 s.replace:
2015-01-22 22:56:57.006input.min.js?ver=5.1.7:588 undefined
2015-01-22 22:56:57.007input.min.js?ver=5.1.7:592 Uncaught TypeError: undefined is not a function
Hmmm.. looks like there was a conflict with this add-on:
https://github.com/kevdotbadger/acf-star-rating
Disabling the plugin looks to have solved the problem. Can you try to duplicate the issue by trying that plugin?
Thanks,
Jesse
One possible route is to query for the Department Head first, and then do a second query to get the remaining posts excluding the Department Head post id. It would be using the exclude
parameter from get_posts
– http://codex.wordpress.org/Template_Tags/get_posts
What version of ACF are you using? In ACF PRO at least, once a page is selected a little X appears that when clicked removes it.
Getting another one in Safari;
TypeError: undefined is not a function (evaluating 'e.replace("_","-")')
http://localhost:8888/wordpress/wp-content/plugins/advanced-custom-fields-pro/js/input.min.js?ver=5.1.7 line 1
revolution slider had this function : getPostsByCategory.
with this query arg :
$query = array(
‘order’=>$direction,
‘posts_per_page’=>$numPosts,
‘showposts’=>$numPosts,
‘post_type’=>$postTypes,
);
my post has a ACF date repeater field (thedates) and i need to sort with first date of each one .
i tried to add this :
‘meta_key’ =>’thedates_0_thedate’,
‘orderby’ => ‘meta_value_num’,
but how could i filter to not display past post ? only if post >= today date ?
To display the date on slide i use the meta tag Revolution slider %meta:thedates_0_thedate% but the format is YYYYMMDD, i need to change it.
And %meta:mycustomtaxonomy% return and display the ID but not the name…
THANKS !!!
As it turns out my host needed me to add the IP to my /etc/hosts file and point to my domain, as I was using the test url given to me when I setup the account.
So on my shared hosting I had a test url of 101.111.111.12/~testing which works fine, but a recent update to mod_security causes issues if you use this URL and post an iframe into a custom text field. It triggers a 403 error and some sort of CORS issue arrises.
I’m really clueless with sysadmin so a quick call to my host was able to sort this out for me.
Moral of the story if setting up a testing url with a shared host, add that IP to your local /etc/hosts file to get rid of any headaches and that will make the site behave as expected, for example if you setup a new account for testing.com and get given a temporary url of 100.100.11.11/~testing edit your /etc/hosts file to have 100.100.11.11 http://www.testing.com testing.com
I hope this helps someone else who isn’t great with sysadmin like me.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.