I am trying to retrieve all advanced custom fields tied to a particular page. This is different than iterating through posts, I am familiar with the following:
$posts = get_posts(array(
'post_type' => 'post_name',
'meta_key' => 'color',
'meta_value' => 'red'
));
However this method is specific to posts and does not allow me to retrieve all ACF by page name.
I appreciate any suggestions on how to accomplish this.
The are too ways to do this that come to mind…
1. Using the Loop
Using WP_Query you can do something like this…
In place of the ‘homepage’ in
'pagename' => 'homepage'
, you want to put the page slug of your page. And of course in place ofthe_field( "text_field" );
you want to add your fields/content.You can also query by Page ID and some other parameters. You can find all other parameters that you can use here:
https://codex.wordpress.org/Class_Reference/WP_Query#Post_.26_Page_Parameters
2. Adding second parameter to the_field() function
More simple way, without custom loop, is just to use ACF’s built-in
the_field()
function with the$post->ID
parameter added as a second parameter.This might be the way to go since you want to show the content of only one page, and therefore don’t really need to loop.
Reference: http://www.advancedcustomfields.com/resources/how-to-get-values-from-another-post/
You can use ACF’s
get_fields()
function –You could then loop through them or simply print the array for testing.
http://www.advancedcustomfields.com/resources/get_fields/