If I set post-status to 'publish' (as shown below) this form creates the post, but my acf fields for this post type (some should be auto-populated) aren't added to the database
If I set post-status to 'draft' this form creates the draft - if I edit and save that draft all is good
Any ideas?
Thanks, Richard
<?php acf_form_head(); ?>
<?php get_header(); ?>
<div id="primary" class="rs-add-forms">
<div id="content" class="site-content" role="main">
<?php while ( have_posts() ) : the_post(); ?>
<?php acf_form(array(
'post_id' => 'new_post',
'post_title' => true,
'new_post' => array(
'post_type' => 'people',
'post_status' => 'publish'
),
'fields' => array('field_5ed5c2215be79',),
'submit_value' => 'Create Person',
'html_submit_button' => '<input type="submit" class="rs-add-button" value="%s" />',
'updated_message' => false
)); ?>
<?php endwhile; ?>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_footer(); ?>
Since acf_form calls acf_add_local_field on the value of the fields key in the options array, you should try structuring your data like the function parses defaults for, internally.
// Apply default properties needed for import.
$field = wp_parse_args($field, array(
'key' => '',
'name' => '',
'type' => '',
'parent' => '',
));
Therefore, you might modify to
'fields' => array(
array(
'key' => 'field_5ed5c2215be79',
'name' => 'Form Field Title',
'type' => 'registered_acf_fieldtype',
'parent' => '[probably optional]'
)
),
Related
I have a custom post type which takes advantage of:
do_shortcode('[products ids="'.$IDs.'"]')
Where it loads certain products depending on the query, the trouble is the WooCommerce stylesheet is not being loading, and the classes are not being added to the body tag. Whats the best way to carry this out, my custom post type template is:
<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Direct access not allowed.
get_header();
?>
<div class="container single-artist">
<?= the_post_thumbnail(array('class' => 'img-responsive img-circle')) ?>
<h1><?= the_title(); ?></h1>
<?= the_content(); ?>
<?php
$products = get_posts(array(
'fields' => 'ids',
'meta_query' => array(
array(
'key' => 'artist',
'value' => get_the_ID(),
'compare' => 'LIKE'
),
),
'numberposts'=> -1,
'posts_per_page'=> -1,
'post_status' => 'publish',
'post_type' => 'product',
));
$IDs = implode(',', $products);
if(count($products)) echo do_shortcode('[products ids="'.$IDs.'"]');
?>
</div>
<?php
get_footer();
So it is getting a related ACF field from the product.
I created a custom field with 'domain_url' id to add field in my taxonomy using the following code:
acf_add_local_field_group(array(
'key' => 'group_6294fa89c564b',
'title' => 'Domain url Field',
'fields' => array(
array(
'key' => 'field_629504c3cdd67',
'label' => 'domain url',
'name' => 'domain_url',
'type' => 'text',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'maxlength' => '',
),
),
'location' => array(
array(
array(
'param' => 'taxonomy',
'operator' => '==',
'value' => 'news_source',
),
),
),
));
And I want to get the value inside the categories foreach, I tried this code but it didn't give a result
<?php
$args = array(
'taxonomy' => 'news_source',
);
$cats = get_categories($args);
foreach($cats as $cat) {
?>
Name: <?php echo $cat->name; ?> >
Domain: <?php echo get_field('domain_url' ); ?> <br/>
<?php
}
?>
Is the problem in the code? Or does the extension not support it?
You are missing an argument in your get_field(). You need to add an arg for the term or term object.
<?php
$args = array(
'taxonomy' => 'news_source',
);
$cats = get_categories($args);
foreach($cats as $cat) {
?>
Name: <?php echo $cat->name; ?> >
Domain: <?php echo get_field('domain_url', $cat ); ?> <br/> // Put the term in here.
<?php
}
?>
In this case you may need an extra query, or to use the term string plus the term ID in a string.
This ACF documentation article shows some different methods of retrieving tax term fields as well.
I am building a website using Advanced Custom Fields that has yachts for sale. The yachts use a custom Post type called ‘yachts’
One of the fields in the individual yacht listing is a true/false value to determine wether it should be a featured listing. (it was originally a checkbox but I changed it after experimenting with the answer in this post)
Advanced custom fields: can't query posts by custom field
I am trying to display previews of the featured listings on the home page but I can’t get it to work.
I originally tried this code from the ACF documentation
edit: I started with just trying to fetch the title but I also need the additional fields
<section class="featured-yachts">
<h2>FEATURED YACHTS</h2>
<?php
// args
$args = array(
'numberposts' => -1,
'post_type' => 'yachts',
'meta_key' => 'featured',
'meta_value' => 'yes'
);
// query
$the_query = new WP_Query( $args );
?>
<?php if( $the_query->have_posts() ): ?>
<ul>
<?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
<?php wp_reset_query(); // Restore global post data stomped by the_post(). ?>
</section>
And now have this after switching to a true false field
<section class="featured-yachts">
<h2>FEATURED YACHTS</h2>
<?php
$args = array(
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'featured',
'value' => '1',
'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();
endwhile;
wp_reset_postdata();
}
?>
</section>
I have also tried editing this: How filter custom posts by Advanced Custom Fields checkbox
but again, I cant get it to work.
The most any of these return is the h3 title
I can't work out what the issue is.
Thanks in advance for any help
edit : I have at least one post using the custom post type 'yachts' that is set to true for featured.
I still have lots of fields to add, but I would have expected that this output the h3 title and then the title of the post marked featured.
In the end, I would like it to function much like latest post previews, but display custom fields and only if the 'featured' true/false is set to yes
I have made an ACF field called 'featured yachts' and registered the block with Gutenberg, but there are no actual ACF fields within it, it's only used to call the file 'content-featured-yachts-block.php' which has this code I am trying to fix.
I am trying to populate the post previews within this featured yachts block with data pulled from the individual yacht listings. In these listings is the 'featured' true/false option. Screen shot atttached.
This is my code for registering the CPT
// Our custom post type function
function create_posttype() {
register_post_type( 'yachts',
// CPT Options
array(
'labels' => array(
'name' => __( 'Yacht Listings' ),
'singular_name' => __( 'Yacht' ),
'supports' => array( 'title', 'editor', 'comments', 'excerpt', 'custom-fields', 'thumbnail' ),
'add_new' => __( 'New Yacht Listing'),
'add_new_item' => __( 'Add New Yacht Listing'),
'edit_item' => __( 'Edit Yacht Listing'),
'new_item' => __( 'New Yacht Listing'),
'view_item' => __( 'View Listings'),
'search_items' => __( 'Search Listings'),
'not_found' => __( 'No Yacht Listings Found'),
'not_found_in_trash' => __( 'No yacht Listings found in Trash')
),
'public' => true,
// 'has_archive' => true,
'rewrite' => array('slug' => 'yachts'),
'show_in_rest' => true,
'menu_icon' => 'dashicons-sos',
'hierarchical' => true,
'taxonomies' => array('category')
)
);
}
Another edit - I made the assumption that if I could get the title field to show, the other fields would be easy but i can't get them either so I ALSO need to work out how to add the additional fields from the listing
The code I have tried is below
<h2>FEATURED YACHTS</h2>
<?php
$args = array(
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'featured',
'value' => 'yes',
'compare' => 'LIKE',
)
),
);
$my_posts = new WP_Query($args);
if ($my_posts->have_posts()) {
while ($my_posts->have_posts()) : $my_posts->the_post();?>
<h2><?php the_title(); ?></h2>
<?php
get_post_meta( get_the_ID(), 'price', true );
?>
<?php endwhile;
wp_reset_postdata();
}
?>
AND ALSO
<section class="featured-yachts">
<h2>FEATURED YACHTS</h2>
<?php
$args = array(
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'featured',
'value' => 'yes',
'compare' => 'LIKE',
)
),
);
$my_posts = new WP_Query($args);
if ($my_posts->have_posts()) {
while ($my_posts->have_posts()) : $my_posts->the_post();?>
<h2><?php the_title(); ?></h2>
<p><?php the_field( 'price' ); ?></p>
<?php endwhile;
wp_reset_postdata();
}
?>
</section>
You can use 'meta_query' and for ACF true and false you don't have to compare with LIKE. Try the below code.
<section class="featured-yachts">
<h2>FEATURED YACHTS</h2>
<?php
$args = array(
'post_type' => 'yachts',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'featuredyacht',
'value' => '1',
)
),
);
$my_posts = new WP_Query($args);
if ( $my_posts->have_posts() ) {
while ( $my_posts->have_posts() ) : $my_posts->the_post();
echo "Title - ".get_the_title()."</br>";
echo "Price - ".get_field( "price", get_the_ID() )."</br>";
echo "Length - ".get_field( "length", get_the_ID() )."</br>";
echo "Year built - ".get_field( "year_built", get_the_ID() )."</br>";
echo "Capacity - ".get_field( "capacity", get_the_ID() )."</br>";
endwhile;
wp_reset_postdata();
}
?>
</section>
To use multiple custom field parameters in your query, replace $args as in Bhautik's answer with something like this:
$args = array(
'post_type' => 'yachts',
'posts_per_page' => -1,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'featuredyacht',
'value' => '1',
),
array(
'key' => 'price',
'value' => 50000,
'type' => 'numeric',
'compare' => '>=',
)
),
);
The above example fetches only yachts with featuredyacht set to 1 or true AND with a price of 50,000 or more. Adjust as needed to suit your parameters.
For example, to query featured yachts within a specific price range:
$args = array(
'post_type' => 'yachts',
'posts_per_page' => -1,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'featuredyacht',
'value' => '1',
),
array(
'key' => 'price',
'value' => array( 50000, 100000 ),
'type' => 'numeric',
'compare' => 'BETWEEN',
)
),
);
The developer documentation on WP_Query should be very useful to you for any further customizations.
I want to show all blog categories that aren't of type gallery. My code almost works but if I have 2 posts in the same category the category is shown twice ie:
If I create a category called 'news' and add 2 non-gallery posts, it shows up as:
news
news
instead of just
news
<?php
$galleryPosts = new WP_Query(array(
'post_type' => 'post',
'order' => 'ASC'
));
?>
<?php if ( $galleryPosts->have_posts() ) : ?>
<?php while ( $galleryPosts->have_posts() ) : $galleryPosts->the_post(); ?>
<?php if(!has_post_format('gallery')) {
the_category();
}
?>
<?php endwhile; ?>
<?php endif; ?>
To suppress non-gallery posts, try selecting only those posts initially and then remove the has_post_format('gallery') check:
$posts = new WP_Query(
array(
'post_type' => 'post',
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array(
'post-format-gallery'
),
'operator' => 'NOT IN'
)
)
)
);
Then in PHP:
if ($posts->have_posts()) {
while ($posts->have_posts()) {
$posts->the_post();
the_category();
}
}
i am using below code that seems to work fine on my local server but when i use it live server , it just displays first page content on every page .
content does not changes on moving to next page while , pagination index show currnet page index though.
below is my code :
<?php
/*
Template Name: My News
*/
get_header();
// First, let's see if we have the data in the cache already
$the_query = wp_cache_get('m-in-news'); // the cache key is a unique identifier for this data
if ($the_query == false) {
// Looks like the cache didn't have our data
// Let's generate the query
$args = array(
'posts_per_page' => 10,
'meta_query' => array(
array(
'key' => 'pdf_cin_file',
'compare' => '!=',
'value' => '',
),
),
'orderby' => 'post_date',
'paged' => $paged,
'order' => 'DESC',
'post_type' => 'minnews',
'post_status' => 'publish',
'cache_results' => true
);
$the_query = new WP_Query($args);
// Now, let's save the data to the cache
// In this case, we're telling the cache to expire the data after 300 seconds
wp_cache_set('m-in-news', $the_query, '', 300); // the third parameter is $group, which can be useful if you're looking to group related cached values together
}
$max_page = $the_query->max_num_pages;
$big = 999999999;
?>
<div class="master">
<?php get_sidebar('presslistpages'); ?>
<div class="rightSection">
<h1><?php echo esc_html(get_the_title()); ?></h1>
<div class="mNews">
<div class="data_gird_content">
<ul>
<?php
// Once we're here, the $query var will be set either from the cache or from manually generating the WP_Query object
if ($the_query->have_posts()) {
while ($the_query->have_posts()) {
$the_query->the_post();
$pdflink = get_post_meta(get_the_ID(), "pdf_cin_file");
$pdflink = isset($pdflink[0]) ? $pdflink[0] : '';
?>
<li><div class="icon"><i class="fa fa-angle-double-right"></i></div> <div class="details"><h2><?php echo esc_html(get_the_title()); ?></h2></div></li>
<?php
}
}
wp_reset_postdata();
?>
</div>
<div class="paginationWrapper"><?php
echo wp_kses(paginate_links(array(
'base' => str_replace($big, '%#%', esc_url(get_pagenum_link($big))),
'format' => '?paged=%#%',
'current' => max(1, get_query_var('paged')),
'total' => $max_page
)), array('a' => array_merge(cci_allowed_a_tag_attr(), array(
'href' => array()
)),));
?></div>
</div>
</div>
</div>
<?php
get_footer();
?>