
Hi @jaypeg
Yes, you can use the relationship field in this way. Does that answer your query?
Perhaps you would be interested in using the repeater field + image sub field instead?
Thanks
E

Please use this filter to modify the WP_Query args:
http://www.advancedcustomfields.com/resources/filters/acf-fields-relationship-query/
Thanks
E

Hi @hungrysquirrel,
Sorry! you’re right, the syntax above was incorrect. I’m glad it got you on the right track though.
Did you get it working?
Cheers
Oh wait this might be working…I am new to PHP….
notice the instances of “$currMonth != $date->format(‘F’)”, I imagine this makes the function return a string, is that right?
<?php
$repeater = get_field('race_event');
foreach( $repeater as $key => $row )
{
$column_id[ $key ] = $row['race_date'];
}
array_multisort( $column_id, SORT_ASC, $repeater );
$currMonth = "start";
foreach( $repeater as $row ) :
{
$date = DateTime::createFromFormat('Ymd', $row['race_date']);
$website_url = $row['race_website'];
if ($currMonth != $date->format('F')) :
{
echo '<h2>' . $date->format('F') . '</h2><br>';
$currMonth = $date->format('F');
}
endif;
echo '<ul>';
echo '<li><h2>' . $row['race_name'] . '</h2></li>';
echo '<li>Date ' . $date->format('F d, Y') . '</li>';
echo '<li>Distance ' . $row['race_distance'] . '</li>';
echo '<li>Vertical ' . $row['race_vertical'] . '</li>';
echo '<li>URL <a href=' . $website_url . '> ' . $website_url . ' </a></li>';
echo '<li>' . $row['race_description'] . '</li>';
echo '</ul>';
}
endforeach;
?>
I think this is on the right track but I am getting this error.
Did I miss something in your example?
Fatal error: Function name must be a string
this is my code
<?php
$repeater = get_field('race_event');
foreach( $repeater as $key => $row )
{
$column_id[ $key ] = $row['race_date'];
}
array_multisort( $column_id, SORT_ASC, $repeater );
$currMonth = "start";
foreach( $repeater as $row ) :
{
$date = DateTime::createFromFormat('Ymd', $row['race_date']);
$website_url = $row['race_website'];
if ($currMonth != $date('F')) :
{
echo '<h2>' . $date('F') . '</h2><br>';
$currMonth = $date('F');
}
endif;
echo '<ul>';
echo '<li><h2>' . $row['race_name'] . '</h2></li>';
echo '<li>Date ' . $date->format('F d, Y') . '</li>';
echo '<li>Distance ' . $row['race_distance'] . '</li>';
echo '<li>Vertical ' . $row['race_vertical'] . '</li>';
echo '<li>URL <a href=' . $website_url . '> ' . $website_url . ' </a></li>';
echo '<li>' . $row['race_description'] . '</li>';
echo '</ul>';
}
endforeach;
?>

Hi @hungrysquirrel,
Something like this?
$currMonth = "start"
foreach( $repeater as $row ) :
{
$date = DateTime::createFromFormat('Ymd', $row['race_date']);
if(currMonth != $date->format('F'))
echo '<h2>' . $date->format('F') . '</h2><br>';
$currMonth = $date->format('F');
endif
echo '<li>' . $row['race_name'] . '</li>';
echo '<li>' . $date->format('m-d-Y') . '</li>';
}
endforeach;
Thanks for the quick response! Though passing the page’s post ID as a parameter did in fact make get_fields() behave accordingly by fetching the page’s custom fields, the page’s behavior is still very, very weird. For instance when I output the Array that get_fields() fetches, everything looks normal I get an array that looks like this:
array(3) {
["jumbo_1"]=>
string(96) "http://localhost:8888/juanturcios.com/wp-content/uploads/2014/01/father-and-son-juan-turcios.jpg"
["jumbo_2"]=>
string(101) "http://localhost:8888/juanturcios.com/wp-content/uploads/2014/01/fashion-photography-juan-turcios.jpg"
["jumbo_3"]=>
string(96) "http://localhost:8888/juanturcios.com/wp-content/uploads/2014/01/wedding-brides-green-forest.jpg"
}
That means get_fields() is working properly. HOWEVER, when I go in and key into this array randomly (and again, the same code works in all pages except my blog index listing page home.php) and use the code’s output to generate the link for the banner’s background image, it is blank. Upon inspecting the div I can see that the background: url(”); is in fact blank. So then I decided to instead try to code it manually and used intead the function the_field(‘jumbo_1’) passing it the first image’s field name key so it would maybe display and nothing happened– still blank. Puzzled by this I decided to pass in the field name of a custom field that is available ONLY for single.php blog posts: the_field(‘slider_image_1’); which enables the user to upload an image for an image slider on every blog post. And to my surprise it did show up!
It’s so odd. It seems as if the blog listing page did not have access to its own custom fields (despite the fact that get_fields() is outputting the right field group array) and rather somehow the code can display fields that are not even allowed to be on the blog page.
Any insights on this? Should I change my field location rule logic somehow?
My code is sorting.
My question may not have been clear, sorry.
Is there a way to do grouping within the sorting so all the entries for May are in a group with the “May” heading and all the June dated custom fields are in the “June” grouping?
If so could you give me some tips on how to implement that with my code above?

Hi @WPDragon
This is called a conditional statement:
<?php
$url = get_field('photographer_url');
?>
<p class="image_photographer">Image courtesy of: <?php if( $url ): ?><a href="<?php echo $url; ?>"><?php endif; ?> <?php the_field('image_photographer') ?> <?php if( $url ): ?></a><?php endif; ?></p>

Hi @lowercase
If you are viewing the post of ‘Metallica’, then the current post id can be found like so:
$post_id = get_the_ID();
This is the value you should use to query the posts with!
Change ‘Melbourne’ to $post_id
Thanks
E

Hi @cmegown
You can render the data corerectly like so:
<?php
$products = get_field( 'products' );
$products = array_map('sanitize_title', $products);
$products = implode(' ', $products );
?>
<li class="<?php echo $products; ?>"></li>
Links:
http://codex.wordpress.org/Function_Reference/sanitize_title
http://au2.php.net/array_map
Thank you for your quick response – I will try those methods – and thanks again for making such an awesome plug in!

Hi @chandru998
The above code that I wrote has 0% connection to the WP_Query object. This means you cant modify it via the query code you have above.
You will need to use the code that I wrote above.
Thanks
E

Hi @matoma16
Sub field required validation was added in v1.1.0 of the repeater field. Can you please update your repeater field add-on?
Thanks
E

Hi @cyberwani
Not at all, you can include ACF4 in your theme without any premium add-ons and no license is required.
Thanks
E

Close, but change:
the_post_thumbnail( '$size' );
to
the_post_thumbnail( $size );
Please be aware, that you have placed the $size = get_field('image_size'); OUTSIDE of your $myposts loop, so this value is not coming from the post itself, but from what ever the template file is querying.
Thanks
E

Is your above code working? Are you sorting successfully?
To link to different parts on the page, you can use an anchor tag using a name attribute and #target. Read more here
http://www.echoecho.com/htmllinks08.htm
Thanks
E

Hi @realph
Thanks for the question, it is very clear to see what you are attempting to do.
The above will not be easy, but it is not impossible. Lets start with filtering the products that are ‘blue’.
You can use this tutorial here to query sub field values:
http://www.advancedcustomfields.com/resources/tutorials/querying-the-database-for-repeater-sub-field-values/
This tutorial may seem a bit complex, but your task here is to find all the $rows in the postmeta table, where ‘blue’ was selected.
Withing the $rows loop, you can find the post_id like so $post_id = $row->post_id;
This allows to get the product ID that contains this ‘blue’ color. Instead of assigning this to a variable called $post_id, you should append it to an array like so: $ids[] = $row->post_id;
After your loop is complete, you should have a populated $ids array which you can now use in a WP_Query argument in the ‘posts__in’ arg!
To show the blue image as the featured one, just loop through all the repeater rows and then match the ‘product_colour’ value to the current queried color. When a match is found, display that image!
Thanks
E
Hi @elliot — This was solved, but just had one quick question related to this…The code above is working perfectly, but I noticed if I put the Photographers Name, without a URL — the Photographers Name will still be a hyperlink & it will reopen the same post page its on…Is there a way, that if I do not include a URL, that it will only display the Photographer name as plain text, and not as a hyperlink?
Hope that makes sense. thanks.

Hi @elliot
I still don’t really follow how that will work. In the example, that requires you put in a key value of ‘Melbourne’. That is great if you only have one value called ‘Melbourne’, but I have 30 different artists.
How can I tell which artist page I am currently on before I show the value for that particular artist without setting up 30 different templates?
Does that make sense?
If I am on the Metallica page, I ONLY want to show Metallica releases. With 30 artists, do I need to set up 30 templates to call the specific artist value on each one?
Hi Elliot,
Thanks for your reply, I know it isn’t a ‘regular’ wordpress query. I’m just trying to get it working like one.
I got my following code working with a custom query_var called albums: Pastebin
And I can use the url: http://mydomain.local/pagename/?albums=2
The last thing I want to achieve is a rewrite rule for the query var, but somehow that isn’t working yet. I would like an url like this: http://mydomain.local/pagename/page/2/
/* Photo albums pagination parameter */
function add_albums_query_var(){
global $wp;
$wp->add_query_var('albums');
}
/* Rewrite rules albums pagination */
function add_albums_rewrite_rule() {
add_rewrite_rule('^/page/([0-9]+)/?$', 'index.php?p=25&albums=$matches[1]', 'top');
}
add_filter('init', 'add_albums_query_var');
add_action('init', 'add_albums_rewrite_rule');
25 is the page id where the repeater field is looped.
Any idea why this isn’t working?
Thanks a lot!
Do you mind giving me any tips on formatting and code structure? I am kinda new to PHP and although this works I am not confident.
One other question I have is, can you recommend a way to approach a UI that would let a user jump to points on the page that are grouped into the same month? Back story is each repeater will have a name and date and now, since I can sort the list by date I was hoping to make it easier for the user to jump to the content for August.
<!-- repeater -->
<?php
$repeater = get_field('race_event');
foreach( $repeater as $key => $row )
{
$column_id[ $key ] = $row['race_date'];
}
array_multisort( $column_id, SORT_ASC, $repeater );
foreach( $repeater as $row ) :
{
echo '<li>' . $row['race_name'] . '</li>';
$date = DateTime::createFromFormat('Ymd', $row['race_date']);
echo '<li>' . $date->format('m-d-Y') . '</li>';
}
endforeach;
?>
<!-- /repeater -->

Hi @hungrysquirrel,
It is possible to sort by date with the above method.
Let me know if you have any problems 🙂
Cheers
Thank you for your quick response.
I was trying to emulate something like on this site:
http://thereason.us/
using a combination of the gallery add-on and jquery masonry.
Please let me know if you have any recommendations.
PS – I love your plug ins!
Ah!
Also, check that you don’t have any ‘pre_get_post’ filters that are modifying the args
That put me int he right direction. none of the other orderby arguments seem to work.
I changed up the query to:
$args = array(
'post_type' => 'portfolio',
'posts_per_page' => '-1',
'meta_key' => 'date_received',
'orderby' => 'meta_value_num',
'order' => 'DESC'
);
remove_all_filters('posts_orderby');
$query = new WP_query($args);
And that fixed it. If this was in the documentation, my apologies for not seeing it. Either way, I’m good to go and thank you for your help.
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.