-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Retrieving posts from category with ACF content intact #32
Comments
Hey! There are two parts I see to this question:
function serialize_front_page_posts($post_data){
// ignore if not front page
if($post_data->ID != get_option('page_on_front')) {
return $post_data;
}
// your $args, with 'cat' changed to 'category' for get_posts
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'category' => 3
);
// get posts in category
$front_page_posts = get_posts($args);
// save results in top-level property called "frontPagePosts"
$post_data['frontPagePosts'] = array();
// attach serialized post data
foreach($front_page_posts as $front_page_post) {
$post_data['frontPagePosts'][] = apply_filter('rez_serialize_post', $front_page_post);
}
return $post_data;
}
add_filter('rez_serialize_post', 'serialize_front_page_posts'); Give that a try in your
/*
* Serialize ACF content with get_field to respect ACF custom formatting
*/
function serialize_custom_acf_content($post_data){
// check to make sure we have ACF installed
if (!function_exists('get_fields')){
return $post_data;
}
// get ACF fields attached to target post
$acf_fields = get_fields($post_data['ID']);
// prep to save serialized fields
$post_data['acf'] = array();
if( !empty($acf_fields) ){
// serialize ACF fields
foreach( $acf_fields as $name => $value ){
$post_data['acf'][$name] = $value;
}
}
return $post_data;
}
add_filter('rez_serialize_post', 'serialize_custom_acf_content'); We actually just added this second function to the Vuepress boilerplate here, so if you're using a new-ish version you should already be good to go! If not, you can just paste the code above into Let me know if there's any problems there and thanks for reaching out! |
Thanks Sander, I'm getting closer thanks to your help! To get it working, I had to adjust like so: Custom Filter
Using I can see my ACF content via |
That absolutely works! I personally usually think of the You're definitely not wrong in thinking that ACF content should live in the // get ACF fields attached to target post
$acf_fields = get_fields($post_data['ID']); and see what you get from that? Thanks! |
Hmm I assumed the same, It wouldn't work when putting Just noticed that I'm getting an Var Dump Although, if I go and assign an ACF to the home/front page then this data will actually show/repeat within the Thanks for your help Sander, sorry to drag out! 🤔 |
Not a problem at all, happy to help! 😄
function test_serialization($post_data){
$post_data['test_generic'] = 'works';
return $post_data;
}
add_filter('rez_serialize_post', 'test_serialization');
function test_serialization_front_only($post_data){
$front_id = get_option('page_on_front');
if($post_data['ID'] == $front_id || $post_data->ID == $front_id){
$post_data['test_front'] = 'works';
}
return $post_data;
}
add_filter('rez_serialize_post', 'test_serialization_front_only'); Any page you load should then have the
function serialize_custom_acf_content($post_data){
// check to make sure we have ACF installed
if (!function_exists('get_fields')){
return $post_data;
}
echo $post_data['ID'];
echo $post_data->ID;
// etc... Let me know if any of those shed any more light on the issue! |
@mrkylemac Any luck with this? Just wanted to check in! |
Hey Sander! I'm unable to serialize any data using If I test the serialization using I'm still quite confused, apologies! |
Apologies on our end! I'm not quite sure why this isn't working either, but I'll look some more into it at the start of next week - it might be a symptom of a bigger issue so it'll be good to learn more about. Is there any chance you have a public repo for the site you're working on? No problem if not, but it might be useful. Thank you! |
No worries Sander, I’ve managed to find a temporary fix.
But, next week let me make my repo public just incase this happens to be a
bigger issue..
Thanks again!
On Sat, 29 Sep 2018 at 4:55 am, Sander Moolin ***@***.***> wrote:
Apologies on our end! I'm not quite sure why this isn't working either,
but I'll look some more into it at the start of next week - it might be a
symptom of a bigger issue so it'll be good to learn more about. Is there
any chance you have a public repo for the site you're working on? No
problem if not, but it might be useful. Thank you!
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#32 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ADGhGEo4dw-FrXXsDuholkNN0JfA44Ceks5ufnCdgaJpZM4WthXC>
.
--
*•*
Mr Kyle Mac
Brand, Interaction & Web Development
+61 (0) 473 431 067
Mitchell House, Lvl 5, Studio #505
358 Lonsdale St, Melbourne, Victoria 3000 Australia
www.mrkylemac.com
Instagram <http://www.instagram.com/mrkylemac.work>, Twitter
<http://www.twitter.com/mrkylemac>, Dribbble
<http://www.dribbble.com/mrkylemac>
|
Hey, I'm reaching out for help more so than submitting an issue here!
I'd like to:
• Retrieve all posts from a specific category and include each post's ACF content (not as meta if I can help it).
Basically, I have a bunch of project posts, all with a 'featured' category that I'd like to feature on my FrontPage.
I would normally write something like:
I'm wondering how I could achieve this with a custom Rest Easy filter or any other suggested method?
Thanks a million!
The text was updated successfully, but these errors were encountered: