Get ACF gutenberg block data from custom post type - advanced-custom-fields

I running into some problems with ACF Gutenberg blocks.
I have registered a Gutenberg block to be used in a custom post type called "Portfolio"
Through a normal wp_query i can display the custom post type on the homepage. But i can not get the ACF Gutenberg block data to show?
Below is the code im currently using.
<?php
// WP_Query arguments
$args = array(
'post_type' => array( 'portfolio' ),
'post_status' => array( 'publish' ),
'nopaging' => false,
'posts_per_page' => '5',
);
// The Query
$query_portfolio = new WP_Query( $args );
// The Loop
if ( $query_portfolio->have_posts() ) {
while ( $query_portfolio->have_posts() ) {
$query_portfolio->the_post();
}
// ACF group
$content = get_field('content');
?>
<!-- // ACF field from within group NOT SHOWING -->
<h1><?php echo $content['title']; ?></h1>
<h2><?php the_title(); ?></h2>
<?php
} else {
// no posts found
}
// Restore original Post Data
wp_reset_postdata();
?>
Im stuck at this point. Somehow i can not get the Gutenberg Block fields to show within the query on the homepage.
All help is appreciated.
Thanks

Related

How to add pagination to recent post list by WP Bakery

I tried to add pagination for the latest post list but keep failed. I'm using an Ocean theme and this is what the code looks like:
[sermons_post idcate_sermon="praise" number="5"]
But it only shows 5 posts and there is no pagination option. So, I'd like to add pagination here for the sermon_post list.
I don't have much background in the HTML or CSS code and don't know where I start first. I feel like the code below is a kind of solution for this but I even don't know where I put this code.
if ( ! function_exists( 'post_pagination' ) ) :
function post_pagination() {
global $wp_query;
$pager = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $pager, '%#%', esc_url( get_pagenum_link( $pager ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages
) );
}
endif;

Timber: How to query posts of a single custom post type in archive.php

I'm using Timber for Wordpress and when I use a custom query in archive.php to get posts of a custom post type, it returns posts of multiple post types.
I tried the exact same query in page.php and it worked perfectly.
Here's the query I'm using:
global $paged;
if (!isset($paged) || !$paged){
$paged = 1;
}
$args = array(
'post_type' => 'horse',
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page' => 20,
'paged' => $paged
);
$context['horses'] = new Timber\PostQuery( $args );
I would expect that to return all items of post type 'horse', but other post types are also mixed in.
Any ideas why this might be happening?
I'm not sure if this is related, but I also added this to functions.php, inside the StarterSite class, in order to add my custom post type to the archive page:
function namespace_add_custom_types( $query ) {
if( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {
// Get all your post types
$post_types = get_post_types();
$query->set( 'post_type', $post_types );
return $query;
}
}
And this was added to the existing function named __construct:
add_filter( 'pre_get_posts', array($this, 'namespace_add_custom_types') );
How are you calling it in the twig file?
{% for post in horses %}
{{ post.name }}
{% endfor %}
Also, are you sure the post type name is horse and not horses? If the wrong post type name is included in 'post_type' it would show all post types.

Advanced custom fields: can't query posts by custom field

I'm trying to query posts whose ACF field "show_on_frontpage" value is equal to "yes" (see definition of this field in screenshot below). As prescribed in ACF docs here's my code:
$args = array(
'posts_per_page' => -1,
'meta_key' => 'show_on_frontpage',
'meta_value' => 'yes'
);
$my_posts = new WP_Query($args);
if ($my_posts->have_posts()) {
while ($my_posts->have_posts()) : $my_posts->the_post();
if (get_field('show_on_frontpage')) the_field('show_on_frontpage'); ?>
endwhile;
}
This returns/displays nothing. If I used instead simply $args = array('posts_per_page' => -1); then I get all my posts and "yes" shows up for those that have "yes" as the value of their "show_on_frontpage" field.
What's wrong with my code?
According to this question/answer on the ACF forum:
https://support.advancedcustomfields.com/forums/topic/using-checkbox-fields-in-custom-queries/
It would be better to switch your checkbox field to a True/False field instead, since it appears that your checkbox group field only contains a single option.
Checkboxes are stored as serialized data and you’re not going to be able to use WP_Query to filter by a checkbox field.
If you use a true/false field then you can use WP_Query, the values of
a true/false field are 0 (zero) for false and 1 for true.
So if you switched your checkbox field to a True/False field, you would rewrite your code as follows:
$args = array(
'posts_per_page' => -1,
'meta_key' => 'show_on_frontpage',
'meta_value' => 1 /* or true */
);
$my_posts = new WP_Query($args);
if ($my_posts->have_posts()) {
while ($my_posts->have_posts()) : $my_posts->the_post();
/* My content for each post with the checkbox checked goes here */
endwhile;
}
This should work if you use the more recent meta_query => array() syntax:
$args = array(
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'show_on_frontpage',
'value' => 'yes',
'compare' => 'LIKE',
)
),
);
$my_posts = new WP_Query($args);
if ($my_posts->have_posts()) {
while ($my_posts->have_posts()) : $my_posts->the_post();
echo get_the_title();
// Post stuff
endwhile;
/* Restore original Post Data */
wp_reset_postdata();
}
Note that you need to give the post ID to the ACF helper functions get_field() & the_field() within the while loop.
See https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters
On a wider note, this article calls into question the wisdom of using post_meta keys for this purpose and is worth a read: https://tomjn.com/2016/12/05/post-meta-abuse/. The article suggests using a custom taxonomy to achieve what you need - being better for performance.

Wordpress custom page template returns the theme headers

I want to return a JSON array from a Wordpress website. I know this can be done using plugins like WP REST API and JSON API, but I am dealing with custom post types so I want to avoid complexity and write the queries myself.
I have overridden one of my pages by writing a new php file in my theme directory called page-345.php.
Inside my PHP file I have the following code
$args = array(
'post_type' => 'partner',
'post_status' => 'publish',
'posts_per_page' => -1 // all
);
$query = new WP_Query( $args );
$cars = array();
while( $query->have_posts() ) : $query->the_post();
// Add a car entry
$cars[] = array(
'name' => get_the_title()
);
endwhile;
wp_reset_query();
wp_send_json( $cars );
I code works and I get output in JSON. However, JSON in returned alongside the header of the template. (See screenshot)
I also tried entering the code
header( 'Content-type: application/json' );
at the top of my page, but then Wordpress returns an error saying that headers have already been set.

Google Map V3 Cakephp helper and multiple markers

I am using the Cakephp Google Map V3 Helper. I can get the google map to show up but the markers do not. Here is my view code:
<?php
echo $this->GoogleMapV3->map();
foreach ($allcondos as $condo) {
$options = array(
'lat' => $condo['Unit']['lat'],
'lng' => $condo['Unit']['lon']
);
$this->GoogleMapV3->addMarker($options);
}
?>
I know that if I just tell the app to echo out my $condo['Unit']['lat'] or ['lon'] it will do so in the foreach loop (so it is pulling my data). What I don't know how to do is how to write the code for the $options array. I have also tried this:
foreach ($allcondos as $condo) {
$lat=$condo['Unit']['lat'];
$lon=$condo['Unit']['lon'];
$options = array(
'lat' => $lat,
'lng' => $lon
);
$this->GoogleMapV3->addMarker($options);
}
How do I write this correctly?
A couple easy steps to get this to work:
Download
Download from https://github.com/dereuromark/cakephp-google-map-v3-helper and place the GoogleMapV3Helper.php file in /app/view/helper/GoogleMapV3Helper.php.
Load Helper
Either modify your appcontroller so that the top of it reads like the following:
<?php
class AppController extends Contoller{
public $helpers = array('Html','Javascript','GoogleMapV3');
}
?>
Or load it in a single controller by adding it to the helpers array as such:
<?php
class DemoController extends AppContoller{
function map() {
$this->helpers[] = 'GoogleMapV3';
# rest of your code
}
}
?
Include Scripts
Include Jquery in your header. Include the following as well:
<?php
echo '<script type="text/javascript" src="'.$this->GoogleMapV3->apiUrl().'"></script>';
?>
Create Map Container
Put this in your view where you want your map to appear. Feel free to modify the properties of the div.
<?php echo $this->GoogleMapV3->map(array('div'=>array('id'=>'my_map', 'height'=>'400', 'width'=>'100%'))); ?>
Note: you can change the default position of the map by including more options than just 'div':
<?php echo $this->GoogleMapV3->map(array('map'=>array(
'defaultLat' => 40, # only last fallback, use Configure::write('Google.lat', ...); to define own one
'defaultLng' => -74, # only last fallback, use Configure::write('Google.lng', ...); to define own one
'defaultZoom' => 5,
),'div'=>array('id'=>'my_map', 'height'=>'400', 'width'=>'100%'))); ?>
Add markers
Can be in a loop or whatever, but this is done in the view after your container is created.
<?php
$options = array(
'lat'=>40.770272,
'lng'=>-73.974037,
'title' => 'Some title', # optional
'content' => '<b>HTML</b> Content for the Bubble/InfoWindow' # optional
);
$this->GoogleMapV3->addMarker($options);
?>
note: only set the 'icon' key in the array if you want to use a custom image. Otherwise, they will not show up.
Include the script for the markers
<?php echo $this->GoogleMapV3->script() ?>
All done!
Alternately, you can use finalize() instead of script() if you do not want to echo the javascript right away, but write it to the buffer for later output in your layout:
<?php $this->GoogleMapV3->finalize(); ?>
See http://www.dereuromark.de/2010/12/21/googlemapsv3-cakephp-helper/ for details.