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.
Related
i am trying to get all details of my custom post and print selected data from that on my single-product page.
so lets say the custom post is named Games and the product page is the one were you select on of the games to buy.
i am using:
$args = array(
'post_type' => 'games',
)
);
$review_details = new WP_Query($args);
this is getting me most of the information the product instance, although i chose the post type to be games. since in the post i have the age and rating for the games.
How will I be able to get all the details i already have in the Games Post to the single-product page of a game i am selling?
$args = array(
'post_type' => 'reviews',
'meta_query' => array(
array(
'key' => 'games_books', // name of custom field
'value' => '"' . $game_id . '"', // matches exaclty "123", not just 123. This prevents a match for "1234"
'compare' => 'LIKE'
)
));
$review_details = new WP_Query($args);
print_r($review_details);
and when i use print_r($review_details); all the parameters of reviews are empty, but when i do the same from a post then i can get the reviews.
again i need to print this data on the single-product page
It sounds like you have additional meta information for the Custom Post Type 'games'. Outputting this extra information can be done using either:
get_post_meta($post_id, 'key');
See here for more information https://developer.wordpress.org/reference/functions/get_post_meta/
Or if you're using a common custom field plugin, like ACF, you'll need to use get_field()
get_field('key', $post_id);
See here for more info https://www.advancedcustomfields.com/resources/get_field/
To print a data for custom post, you can use WP_Query with While Option.
For example:
<?php
$args = array( 'post_type' => 'games');
$the_query = new WP_Query( $args );
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<!-- Loop Start -->
<?php the_title(); ?>
<!-- Loop End -->
<?php else: endif; ?>
If you want to print custom areas
<?php echo get_post_meta($post->ID, 'key', true); ?>
I have problem with ACF in Wordpress. I created own field And created own plugin to display it, but I don't know how I can display all my fields.
Now I have something like this:
wp_reset_postdata();
$myargs = array (
'showposts' => 6
);
$myquery = new WP_Query($myargs);
if($myquery->have_posts() ) :
while($myquery->have_posts() ) : $myquery->the_post();
?>
<p>
<?php the_title(); ?>
</p>
<?php endwhile;
endif;
wp_reset_postdata();
And this display me all posts. I want to display posts only from category "Raporty" and I want to display all of my custom fields.
Sorry for my english ;)
Siema Kubol,
to show posts only with post_type 'raporty' add it to $myargs:
$myargs = array (
'showposts' => 6,
'post_type' => 'raporty'
);
In wp loop you could just use e.g. get_field( 'typ_raportu' ). Outside loop get_field( 'typ_raportu', post_id_here ).
I want to connect companies with products using Advanced Customs Fields. These are some pages on my website:
www.example.com/companies/apple/
www.example.com/companies/microsoft/
www.example.com/products/iphones/
www.example.com/products/ipods/
For example I want to connect Apple with their products (iPhones and iPods).
I have added a relationship field in ACF that is called related_articles that is only available when the pages parent is the /companies/-page.
So on Apple's company-page - the products iPhones and iPods have been selected in the custom relationship field related_articles.
On page.php I added the following:
<?php
$posts = get_field('related_articles');
if( $posts ): ?>
<?php foreach( $posts as $p ): // variable must NOT be called $post (IMPORTANT) ?>
<?php echo get_the_title( $p->ID ); ?><br>
<?php endforeach; ?>
<?php endif; ?>
On the page www.example.com/companies/apple/the following is returned:
iPhones<br>
iPods<br>
Which works exactly as I want.
The problem is that http://www.example.com/products/iphones/ and http:// www.example.com/products/ipods/ is empty. Here I want the relationship to be reversed and Apple<br> to be displayed. How can I solve this? Is it possible with a code that is compatible in both ways?
I do not wish to make a second "connection", from products to the companies - this needs to be handeled from a single page and not two. Custom post types is also a no go.
Updated - New code from the Querying relationship fields documentation:
<?php
$related_articles = get_posts(array(
'post_type' => 'post',
'meta_query' => array(
array(
'key' => 'related_articles', // name of custom field
'value' => '"' . get_the_ID() . '"',
'compare' => 'LIKE'
)
)
));
?>
<?php if( $related_articles ): ?>
<?php foreach( $related_articles as $article ): ?>
<?php echo get_the_title( $article->ID ); ?><br>
<?php endforeach; ?>
<?php endif; ?>
This dosen't return anything (blank).
This is certainly possible - ACF allows for 2 way or reverse querying of related items. So, your product page, you could do the following (with your own relevant acf names)
global $post;
$related_company = get_posts(
array(
'post_type'=>'company',
'meta_query'=>array(
array(
'key' => 'related_product',
'value' => '"'.$post->ID.'"',
'compare' => 'LIKE'
)
)
));
The whole process is explained in detail on the ACF site: http://www.advancedcustomfields.com/resources/querying-relationship-fields/
Although, whether this is possible without using Custom Post Types, I'm not certain.
I am creating a wp template based on 2013. I want to display a 'page' that has some content from a page and then 6 posts on it. These posts have been selected via the themes options panel using the settings api. So I can get at each one using
$options = get_option( 'osc_theme_options' );
The template i have been using so far is based on a page so i am guessing I need to change the loop in someway.
The loop goes:
<?php /* The loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>...
I want to know how to alter the loop so that it pulls in just the posts that have been selected. At the moment this template/loop is only pulling in the page - which I guess is fair enough.
Would it be possible to create 'another loop' maybe under the first that then brings in the selected posts - if so how?
Many thanks
Yes you can effectively create another loop under the existing loop to display the posts. I am not sure what $options = get_option( 'osc_theme_options' ); returns, i.e an array of Post ID's etc. In order to show the posts you need to do a custom loop:
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() )
{
echo '<ul>';
while ( $the_query->have_posts() )
{
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
}
else
{
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
This is taken from:
https://css-tricks.com/snippets/wordpress/custom-loop-based-on-custom-fields/
Also see the following:
https://digwp.com/2011/05/loops/
http://www.smashingmagazine.com/2013/01/14/using-wp_query-wordpress/
So effectively it all boils down to the $args variable as to which posts you will get. Here is an example of multiple ids
id=2,6,17,38
So if $options = get_option( 'osc_theme_options' ); returns an array of post ids like so:
array(
0=>1
1=>22
2=>27
)
You could probably do something like:
$args = "id=".implode($options,",");
This is the best advice I can give without deeper knowledge of the theme etc.
I have used ACF. I have added the 'taxonomy' select dropdown field to the post edit page. From the dropdown I select which category this post should be featured in, but my code is displaying the same post as featured across all categories.
Below is the code in the category.php file. I need it to display the most recent post which has been given a 'Feature In Category', and to therefore be featured in the category I have defined.
My current loop in category.php
<?php
$category = get_field('feature_in_category');
// args
$args = array(
'numberposts' => -1,
'posts_per_page' => 1,
'category__in' => $category,
'orderby'=> 'modified'
);
// get results
$the_query = new WP_Query( $args );
// The Loop
?>
<?php if( $the_query->have_posts() ): ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="small-6 columns">
<div class="img-box-shadow">
<a href="<?php the_permalink(); ?>">
<?php echo the_post_thumbnail(); ?>
</a>
</div>
</div>
<div class="small-6 columns">
<h3><?php echo the_title(); ?></h3>
<p><?php echo the_excerpt(); ?></p>
</div>
<?php endwhile; ?>
<?php else : echo '<p style="color:#fff;">no posts</p>'; ?>
<?php endif; ?>
<?php wp_reset_query(); // Restore global post data stomped by the_post(). ?>
Pastebin: http://pastebin.com/NR3UanAd
I'm not very familiar with ACF and haven't yet used it. I had a look at some documentation etc, and this is what I've found:
ACF TAXONOMY FIELD
The ACF taxonomy field will return NULL in your application as the ID being passed to get_field is a category ID, which is a non valid post ID. Yes, you can pass a category ID to get_field, but then there must be a custom field assigned to that particular category in order to return the value of that custom field.
Look at the ACF docs
$field = get_field($field_name, $post_id, $format_value);
◦$post_id: Specific post ID where your value was entered. Defaults to current post ID (not required). This can also be options / taxonomies / users / etc
ACCESS STORED DATA BY ACF
As I stated before, I'm not familiar with ACF but it seems that ACF field data is stored in the same way as data is stored by the default custom fields. So you will have a matching pair of key=>value sets. This data can be accessed and retrieved by a meta_query
THE IDEA
The ACF Taxonomy dropdown saves a selected value which translated into a category. Again, I'm not sure whether it saves the name, slug or ID, but from what I can pick up, it saves the category ID, so I will make my assumptions on that
So, the first thing to do here is, get the currently viewed category's ID. This can can be retrieved by get_queried_object_id()
Now that you have the current category ID, you will need to match that to a value for the specific meta_key=feature_in_category and return the posts that has this specififc key=>value pair attached to it
THE CODE
Your code should look something like this with the assumption ACF data is stored in the same way as data is stored from a custom field (This code requires PHP5.4+, for earlier versions, change [] to array())
$cat_id = get_queried_object_id(); // Assumption that category ID is saved by ACF
$args = [
'posts_per_page' => 1,
'orderby' => 'modified',
'meta_key' => 'feature_in_category',
'meta_value_num' => $cat_id, // Assumption that category ID is saved by ACF
];
$q = new WP_Query( $args );
if( $q->have_posts() ) {
while( $q->have_posts() ) {
$q->the_post();
// YOUR TEMPLATE TAGS AND MARKUP
}
wp_reset_postdata();
}
If the category ID is not saved by the ACF field, and it saves the name or slug, you can change the code as follows
$category = get_queried_object()->name; // For category name
or
$category = get_queried_object()->slug; // For category slug
Then in your query arguments, change
'meta_value_num' => $cat_id, // Assumption that category ID is saved by ACF
to
'meta_value' => $category,
EDIT
In addition and posted as a comment, the query aruments as used by the OP, working.
$cat_id = get_queried_object_id();
$args = [
'posts_per_page' => 1,
'orderby' => 'modified',
'meta_query' => [
[
'key' => 'feature_in_category',
'value' => $cat_id,
'compare' => 'IN'
]
]
];
After reviewing the documentation here http://codex.wordpress.org/Class_Reference/WP_Query it appears that your code is doing all the right stuff. I believe your answer lies in the actual query string that is being used to produce your results. You should be able to see that query string in the properties of the query object by doing:
<?php
$category = get_field('feature_in_category');
// args
$args = array(
'numberposts' => -1,
'posts_per_page' => 1,
'category__in' => $category,
'orderby'=> 'modified'
);
// get results
$the_query = new WP_Query( $args );
echo '<pre>'; print_r($the_query); die();
I suggest running the query string once you see it directly on the database using something like phpMyAdmin or Navicat to connect directly to the database. You should be able to adjust your query there to produce your desired results and then adjust your arguments accordingly. It may also be that there is nothing wrong with your query and that the issue lies with the category functionality instead. This debugging method should reveal that as well if that is the case.
UPDATE:
I think you just need to use 'cat' and not 'category__in'. Also, make sure you use what you set for Field Name inside of the call to get_field. This feedback is based on this example: http://www.the-perfect-life.org/html-5-css-3-php-wordpress-jquery-javascript-photoshop-illustrator-tutorial/how-to-create-custom/wordpress-plugin-theme-codex-code-function/advanced-custom-fields-get-field-taxonomy-query-related-posts-by-tag-and-category/
$categoryValue = get_field('feature_in_category');
$args=array(
'cat' => $categoryValue,
'posts_per_page'=>1 // Number of related posts to display
);
$my_query = new wp_query( $args );
while( $my_query->have_posts() ) {
$my_query->the_post();
?>
<div>
<a href="<? the_permalink()?>">
<?php the_title(); ?>
</a>
</div>
wp_reset_query();