In a wordpress theme I am programming, I created a custom post type and its template. This custom post type displays a list thanks to advance custom fields that I attached to the post template. In fact, I used the "repeater" field of Advance custom fields pro. The user only has to insert items of the list when editing the post.
I am trying to do the following: I want to display in two specific pages templates the post with all its custom fields (the list created through the repeater). I am not able to do that, I only can retrieve the "normal" fields of the post (the title, the content...). I created a snippet so you can look at my code.
<?php //the single of the post: ?>
<ul class='slider-partners'>
<?php
//slider partners
if( have_rows('slider_partenaires_hp') ): //"slider_partenaires_hp" is the repeater field
// loop through the rows of data
while ( have_rows('slider_partenaires_hp') ) : the_row();
// display a sub field value
echo "<li class='partenaire-slide'><img src='" . get_sub_field('logo_partner') . "'></li>"; //"logo_partner" is the item inside the repeater field
endwhile;
else :
// no rows found
endif;
?>
</ul>
<?php //the template of the page where I try to retrieve the above post:
$the_query = new WP_Query(array(
'post_type' => 'our-partners',
'posts_per_page' => 1,
'order' => 'DESC'
));
while ( $the_query->have_posts() ) :
$the_query->the_post();
endwhile;
wp_reset_postdata();
?>
Could you please help me in calling a post with all its custom fields in two diferent pages ?
Thanks
Related
I am trying to create simple repeter filed with ACF on my WordPress site. What I trying to get is to let admin display "Other interesting" blog posts at the bottom of the article.
So far I have created a loop with Wp_Query:
<?php
if( have_rows('featured') ): while( have_rows('featured') ) : the_row();
?>
<div class="container-fluid blog-container medium-container">
<div class="row">
<div class="col-12 blog-one">
<?php
// Get ACF sub field
$get_ids = get_sub_field('article_id');
// Make them display with comma
$show_ids = implode(', ', $get_ids);
// Featured blog list query
$blog = new WP_Query( array(
'posts_per_page' => 5,
'order' => 'DESC',
// Display post with specific ID using ACF repeater field inside array
'post__in' => array($show_ids)
));
...
...
...
<?php endwhile; endif; ?>
So the goal is to display numbers (posts ID's) included by admin on backend - liost them in array inside "post__in". But my code does not work. Any ideas how to fix that?
Advanced Custom Fields has a post object field type. You can configure it to accept multiple posts which it will then return as an array of posts objects (WP_Post) for you. This avoids any need for looping through a repeater field to build the arguments for a query.
Recommended Solution
Firstly, replace the repeater field with a post object field. Set the post type to post, allow null to true, and multiple to true.
You can then adapt your code to display those posts.
Example:
<?php if ( $featured_posts = get_field( 'featured' ) ) : ?>
<div class="container-fluid blog-container medium-container">
<div class="row">
<div class="col-12 blog-one">
<?php foreach ( $featured_posts as $post ) {
setup_postdata( $post );
// Do whatever you need to do to display the post here...
} ?>
. . .
<?php endif;
Documentation: https://www.advancedcustomfields.com/resources/post-object/
Fixing Your Existing Code
If you'd instead prefer to fix the code you already have, you need to rethink the loop.
Each time you iterate over your repeater field, you'll go in and grab the ID that was entered (subfield) for the post. You need to collect all of those up first and then use that to build your query.
Example:
$posts_ids = [];
if ( have_rows( 'featured' ) ) : while( have_rows( 'featured' ) ) : the_row();
$post_ids[] = get_sub_field( 'article_id' );
endwhile; endif;
// Let's drop any blank elements and force them all to integers.
$filtered_ids = array_map( 'intval', array_filter( $post_ids ) );
$blog_query = new WP_Query( [
'post__in' => $filtered_ids,
] );
You could add some more checks, clean this up, etc. Either way you're still far better off letting ACF do this for you and opting for the recommended solution instead.
In my sites two custom post type, one is advertiser & 2nd is events, I m display this two CPT display into one slider, not image only content.
2 CPT created in my project,
1)Advertiser 2)Reader
I m getting this two CPT content into one slider
<?php
// The Query
$queryslider= new WP_Query(array( 'post_type' => array( 'Advertiser', 'Reader' ), ));
query_posts( $queryslider);
// The Loop
while ( $queryslider->have_posts() ) : $queryslider->the_post();
echo '<li class="slide">';
the_title();
/* and other content what you want */
echo '</li>';
endwhile;
// Reset Query
wp_reset_query();
?>
Good luck! :)
I am looking to automatically create a Wordpress Menu for the 'current' Custom Post Type. I found a useful snippet that outputs the current custom post type here:-
$post_type = get_post_type( $post->ID );
echo $post_type;
But am struggling to translate this (or alternate method) into a dynamically created menu for the current custom post type - list all posts in the custom post type. I can't do this on an individual custom post type basis as I'm using a master template to display a series of custom post types.
Thanks
Glennyboy
Here's a super simple way to do it. just create a loop that lists all the page titles wrapped in a link.
<?php
$obj = get_post_type_object(get_post_type($post->ID));
echo '<h2>' . $obj->labels->name . '</h2>';
?>
<ul>
<?php
$query = new WP_Query(array('post_type' => get_post_type($post->ID), 'posts_per_page' => -1, 'order' => 'DESC', 'orderby' => 'date',));
while ( $query->have_posts() ) : $query->the_post();
?>
<li><?php the_title(); ?></li>
<?php endwhile; wp_reset_postdata(); ?>
</ul>
This will loop through all the current post's post type and create an unordered list with all the posts titles, wrapped in links to the posts. You could obviously change the HTML to suit your needs.
I am using a ACF select field to enable the page admin to select a category of posts to display under the page content. For example, if he selects "Televisions" in the ACF select field, all posts in that category will display after the page content. Here is the code for that bottom part of the page (after the main content), from the page template:
<h2>Learn more about <?php the_field('main_page'); ?></h2>
<ul>
<?php
$main_page=get_field('main_page');
$related_systems = new WP_Query( 'category_name='.$main_page );
while( $related_systems->have_posts() ) : $related_systems->the_post();
if($post->post_type == 'post'):
?>
<li><?php the_title();?></li>
<?php
endif;
endwhile; ?>
</ul>
Here is the ACF settings screenshot
The select field shows up fine on the admin side in all four pages that have the Main four page template, but both get_field('main_page') or the_field('main_page') end up blank (I tested get_field with echo and nothing shows up). How To get the field value in the page template?
I'm using WordPress 3.8.1 and ACF Version 4.3.5
Inside the main content, add $page_id = get_the_ID();.
And in the custom loop, call the fields:
the_field( 'main_page', $page_id );
get_field( 'main_page', $page_id );
Those functions work without an ID if used inside the loop, otherwise we need to specify what post we are requesting.
Also, you can filter the post type when calling WP_Query:
WP_Query ( 'post_type=post&category_name=' . $main_page );
I have posts with ACF repeater date fields showing the beginning of an event.
Advanced custom fields has a feature which allows posts to have repeater fields added it it. So I added a repeater date field, so one post can have multiple dates associated with it.
Now I want to list events by the date. I figured out, how to list them by the first date, but how would I go on about listing all of the posts in one list?
Here is the code I have until now (I'm a noob at php):
<?php $posts = get_posts(array(
'numberposts' => -1,
'category' => array('8,11,12'),
'orderby_field' => 'trip_dates_0_start', // order of posts by this field
'orderby_type' => 'date',
'order' => 'ASC' ));
if($posts)
{ foreach($posts as $post) : ?>
<?php while(the_repeater_field('trip_dates')): ?>
<?php the_sub_field('start'); ?>
-
<?php the_sub_field('finish'); ?>
<?php endwhile; ?>
<?php the_title(); ?>
<?php endforeach; } ?>
Now one post would repeat the date field, but be sorted by the first one of them. How do I repeat the post and each have one date? Even if you can't help me with the code, I'd like to know the logic behind creating something like this.