Sorry, it was due to a silly oversight.
After trying so many different things I managed to get it to work by simply doing {{post.get_field('calendar_file')}}
.
Yes! It worked!!
Thank you so much.
Hopefully some day I’ll get to the point where I actually notice these small, almost obvious “mistakes” (calling for the child with no child ID). 🙂
Sorry John, forgot to respond!
Yes, that was the error. Sometimes you just need a second pair of eyes 🙂
Thanks!
Okay, I realized I could simply everything by making a few small changes. Instead of making each ‘town’ into a post, I deleted them all and turned each ‘county’ into a post. Then I added a repeater field, so the user creates a list of towns in each county. Pretty simple fix!
That being said, I’m still curious if my original question is even possible and/or even recommended ever.
Just found Posts 2 Posts plugin. Amazing!! I think I’m set. For now. 🙂
Well, hot-dog!
Thanks cabrailsford, it worked like a charm.
I know I’m a little late the show, but I just want to give @James a solid stick-tap for a link to that plugin. Helped me immensely!
Yeah, I’ve tried both of those suggestions previously as well. Still no luck.
The client has requested to go in a much simpler direction dealing only with Towns with nothing else. In a way I’m glad, but I would have loved to figured this out in any case!
I thought of that as well, a while after I posted that last question. Still doesn’t work…
Here’s a gist of all the code for the page, maybe there’s something above this section that is interfering?
https://gist.github.com/swthate/cabf1ffe9ca329e0a834
Thanks for all the help so far. I appreciate it a lot.
I finally got back to this project and tried your suggestion. I closed the main loop and started this one, but <?php the_title(); ?>
is returning the state’s title… not the title of the counties. I might be wrong, but my assumption is that I should be able to use the_title()
because this secondary loop is going through the county post type.
Here’s my code:
<?php
$args = array(
'post_type' => 'county',
'posts_per_page' => -1,
'meta_query' => array(
'key' => 'state',
'value' => $post->ID,
),
);
$query = new WP_Query($args);
if(have_posts()) : ?>
<ul>
<?php while(have_posts()) : the_post(); ?>
<li><?php the_title(); ?></li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
Thanks again!
How would I go about reversing the “relationship” and listing all the Locations in a Town, or all the Counties in a State? Looking through the documentation, I can’t really tell if I need a second field or not. I don’t want one, I feel like there should be a way to harness the connections already made between posts, but I can’t find out how to use it.
Ah-ha, that makes sense. Thank you.
I’m using a Relationship field and returning an Object (inside ACF it calls it a Post Object, which is kind of confusing).
I thought it was needed, after reading the Relationship documentation. I’m still working on my fluency in PHP, so I don’t always catch when something looks nonsensical 😉
The code you pasted looks really good, but sadly when I use it I get the same “Trying to get property of non-object” error for every call to Town, County and State.
I have my relationship fields set to Post Object, so I’m not sure how they can’t be objects…
I turned debugging on, and found these messages:
Trying to get property of non-object ... on line 27
Trying to get property of non-object ... on line 28
These lines correspond to lines 16 and 17 in my gist.
Having an interesting problem while trying to display the results.
I made a gist of my code: https://gist.github.com/swthate/abc35769addcce27c03d
I’m following the basic loop with postdata example from http://www.advancedcustomfields.com/resources/relationship/
It is correctly echoing the title of the related town, but for the county it’s echoing the title of the “Hello World!” example post. Is that because $posts = get_field('town')
and $town = get_field('town')
are so similar? I have no idea. And, interestingly, it echoes the title of the town for the state.
So, this is what the output looks like:
Town: Burgerstadt
County: Hello world!
State: Burgerstadt
When it should look like:
Town: Burgerstadt
County: Nuechtegall
State: Minnesota
And yes, those, save for Minnesota, are filler nonsense names 😉
Thanks!
That makes a bit of sense. As I was reading I was already asking how much this will impact performance. Admittedly, the reason I’m doing this is because the previous solution we used was to place all towns/counties/locations on a couple google maps as pins using a plugin. There are thousands of records, so the site all but stopped working because of the load.
I actually haven’t done much with caching on any WP projects, as they’ve never been this large. Would it be wise to get my queries working on a few sample records, and then shift to caching? Or should I be incorporating that right away?
Thanks for the help!
Thank you,
I have a tendency to try to over-think things 🙂
Thank you so much!
I knew I was going about it wrong in some sort of fashion 🙂
Figured it out 🙂
$county_field = get_field('county', $post->ID);
$county = substr($county_field, 0, -5);
One more follow-up question:
Right now I have a select field with options like “Martin (MN)”. The only reason I have (MN) included is so A) if there were a Martin MN and a Martin WI, these two choices would be different, and B) so the user can tell the difference while selecting counties. However, when displaying the field on my site, I only want to display the name “Martin”, and not “(MN)”. Is the only real solution here some sort of PHP string function, like somehow not displaying the last 5 characters of any county field?
Thanks again.
Cool. I’m glad that wasn’t some fringe idea, then 🙂
Thanks again for the help and input.
Thank you for the ideas.
As for the first question, I’m thinking about moving all counties to one dropdown field. For the values I would use something like “martin_mn” with the label “Martin” to represent Martin County, Minnesota. With the combobox option enabled, I don’t think this would be a problem for data-entry, as the user can simply “search” for the county they want to select.
Silly mistake… on my archive pages I switched from include_once to include, and it works 🙂
If possible, though, I’d still like to know if anyone knows how I can move these variables and functions over to functions.php and not have a separate file just for this.
I read through this post: http://support.advancedcustomfields.com/forums/topic/how-to-call-acf-fields-from-my-functions-php-file/
I tried using something like $state = get_field('state', $post->ID);
inside functions.php, but it still returned null.
So then I created veterans_functions.php in my theme’s root dir and included that same code, used include_once
in my pages where I wanted to use those variables, and now it works. However, on my archive and index pages each veteran listed there is populated by the data of the most recently added veteran…
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.