Advanced Custom Fields (ACF) Checkbox - Promotion of post to 'top story' - wordpress

I am using the ACF plugin which is awesome but am struggling with a feature of it, namely the checkbox.
I am trying to use the checkbox as a means of promoting a blog post to a 'top story'.
So I have set up a ACF checkbox field called 'top_story' and if it is checked it should promote the post and if it is not checked then it won't promote the post.
Now this does work but I keep getting the following error message whenever a blog post does not have that checkbox ticked.
Warning: in_array() [function.in-array]: Wrong datatype for second argument
I have simplified the code so it looks like this:
<?php
if( in_array( 'topstory', get_field('top_story') ) )
{
echo '<h1>This is a top story</h1>';
}
else
{
echo '<h1>This isn't a top story</h1>';
}
?>
So I guess what I want to know is what is going wrong here and how to rectify it? It looks like as there is no value in the array for the posts which aren't 'top stories' then there is no argument passed into the 'get-field' function and it falls over?
I was just going to hide the errors as essentially it still works but that doesn't sit comfortable with me and I'm sure I will need to do this again in the future.
Thanks for all your time and help in advance.

Maybe like this:
<?php
// args to check if "Top Story" os TRUE:
$args = array(
'cat' => '5', // Enter Category for "Topstories"
'posts_per_page' => 3, // How many posts to show if multiple selected "Backend"
'orderby' => 'date', // How to sort posts - date, rand etc...
'order' => 'asc', // How to order posts - ASC, desc etc...
'meta_key' => 'topstory', // Name of ACF field to filter through
'meta_value' => 'yes' // Yes = Show, No = Don't show
);
// The results:
$the_query = new WP_Query( $args );
// The Loop:
<?php if( $the_query->have_posts() ) :?>
<h1>This is a top story</h1>
<?php
while ( $the_query->have_posts() ) : $the_query- >the_post(); ?>
....
// Properties to show you post //
....
endwhile;
endif;
wp_reset_query(); // Reset/kill query
?>

Sounds like there are two things you could be running into here:
If a field isn't set, or doesn't exist, get_field() will return false.
If none of the options in a checkbox field are checked, get_field() will return an empty string.
In either case, you won't have an array to search with in_array, and will get a warning if you try.
I'd try this, following ACF's documentation. You should also consider using ACF's True/False field, which is designed for this sort of thing; the Checkbox field is more for multiple checkboxes, of which more than one can be true.
<?php
$topStory= get_field('top_story');
if($topStory) // Check whether this meta field exists at all
{
if(is_array($topStory) && in_array( 'topstory',$topStory ) {
echo "<h1>This is a top story</h1>";
}
else {
echo "<h1>This isn't a top story</h1>";
}
}
?>
If you had a True/False field, you would make it a bit simpler:
<?php
if(get_field('top_story')) {
echo "<h1>This is a top story</h1>";
} else {
echo "<h1>This isn't a top story</h1>";
}
?>

Related

Wordpress get_posts() shows only partially correct data of custom post inside user profile

I have a wordpress-based website where I'm trying to move some custom functionality that used to be just plain mysql table and some code to insert/update/show stuff from it. I'm trying to merge it in WP as custom posts so I can use filters, tags, pagination and such.
It supposed to show a few custom items inside the user profile (Ultimate Member). Like, user info, then 10 of user's top items of this kind, then on next tab 10 items of some other kind, etc.
But seems like things aren't as simple in WP, and you can't just toss things on top of each other and expect it to not overlap. >_> So when I'm trying to add a block with custom post type data, it returns only some of the data mixed with profile data mixed with nothingness.
Yeah, I get it, there's probably a profile loop and some data already inside variables, as far as I could understand from manuals. What I can't understand is how to fix it. Here's how it looks:
$args = array(
'author' => $uid,
'numberposts' => 10,
'post_type' => 'ff',
);
$ff = get_posts($args);
if($ff){
foreach($ff as $f){
setup_postdata($f);
the_content(); //shows what's needed, as well as ID, the_time() and some more
the_title(); //shows author's name instead of post title
the_tags(); //shows nothing, as well as excerpt, etc
get_the_excerpt(); //still nothing
$f->post_excerpt; //but this shows the excerpt, as well as print_r($f)
}
wp_reset_postdata();
}
Maybe someone could give a hint what am I missing? Thanks in advance!
Try using the post ID to get your data and echo it. A little more code but may work better.
// Set the global post up here
global $post;
$args = array(
'author' => $uid,
'numberposts' => 10,
'post_type' => 'ff',
);
$ff = get_posts($args);
if($ff){
foreach($ff as $f){
setup_postdata($f);
$id = $f->ID; // get post ID
$content = get_the_content($id); // get the content to echo later
$tags = get_the_tags($id); // use to get tags, these are not part of the get_posts return object
echo $content; // show the content
echo $f->post_title; // show the returned post title. can use get_the_title($id) and echo it if this does not work
// Display the tags after getting them above
foreach ($tags as $tag) {
echo $tag->name . ', ';
}
// You can get the excerpt this way too but you said your other worked okay
$excerpt = get_the_excerpt($id);
echo $excerpt;
}
wp_reset_postdata();
}
(Original) To elaborate. This sets up the global post object. Which is generally a requirement for your functions to work in the loop. Not always the case as I've found over the years, but a good practice if you're not using the post ID in the loop to get your data.

Wordpress standard loop: additional post count with parameters

I am altering a template file of the Search & Filter Wordpress Plugin to show additional post counts.
The plugin uses the standard Wordpress loop to query posts by certain parameters. The number of posts can be counted by using found_posts.
Now I want to display a second post count that takes additional parameters into account, eg. post_status. I have to stick to the regular WP loop to keep the query from the Plugin intact.
Something like this in the commented line:
if ( $query->have_posts() ) {
echo $query->found_posts 'posts found';
// echo $query->found_posts(array('post_status=>'private') 'private posts found';
while ($query->have_posts()) {
$query->the_post();
the_title();
}
}
The code works fine, except for the commented part which obviously doesn't work.
Is there a way to add another post_count with additional parameters to the standard loop?
Thanks
Georg
You could not add like this echo $query->found_posts(array('post_status=>'private') but before your loop just use below code to count posts by status. in your case you want to count posts by status private so add below code.
$args = array('post_type' => 'your_post_type_name','post_status' => array('publish', 'pending', 'draft', 'future', 'private', 'inherit', 'trash')
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
echo $query->found_posts 'posts found';
// echo $query->found_posts(array('post_status=>'private') 'private posts found';
$count_posts = wp_count_posts('post');
$private_posts = $count_posts->private;
echo $private_posts. 'private posts found';
while ($query->have_posts()) {
$query->the_post();
the_title();
}
}
similarly if you want to show count for publish or draft posts you can add below code.
$publish_posts = $count_posts->publish;
$draft_posts = $count_posts->draft;
you could count any other posts by its status is well.

Getting Only Custom Post Types Posts with WP_Query

I am trying to get custom posts with WP_Query but it's not returning only custom post type posts but also default posts too.
I am using
$args = array (
'post_type' => array ('survey')
);
$sPosts = new WP_Query($args);
as a result I am getting 'survey' posts as well as default posts, I only need it to return 'survey' posts.
P.S. when I use query_posts() it returns the required result, but just not getting it done with WP_Query and I prefer to use WP_Query not the query_posts()
Please try it like that....
<?php
$new = new WP_Query('post_type=discography');
while ($new->have_posts()) : $new->the_post();
the_content();
endwhile;
?>
I believe this custom query should actually be your main query, in which case you should not use a custom query
The problem you are facing is either due to
Wrong use somewhere of pre_get_posts. Remember, pre_get_posts alters all instances of WP_Query, backend and front end. Lack of correct use will break all queries
You have not changed the loop to be objects of your new query
To come back to the point, as said, this is suppose to be the main query, so lets target that problem.
The first thing to do would be to delete the custom query. Once you have done this, you would only see normal post.
We are now going to use pre_get_posts to alter the main query to only show custom posts. Paste the following inside your functions.php
add_action( 'pre_get_posts', function ( $q ) {
if( !is_admin() && $q->is_main_query() && $q->is_home() ) {
$q->set( 'post_type', 'YOUR CPT' );
}
});
You should now just see post from your cpt on the homepage
EDIT
Your index.php should look something like this
if( have_posts() ) {
while( have_posts() ) {
the_post();
// HTML mark up and template tags like the_title()
}
}else{
echo 'No posts found';
}
This code can get all the posts in a custom post type..,
wp_query is the function can get all the posts in the custom post type. wp_query requires array, so you can give the custom post type name in post id to array
$args = array(
'post_type' => 'address', //custom post type
'posts_per_page' => -1 // get all posts
);
$query = new WP_Query( $args );
// Check that we have query results.
if ( $query->have_posts() ) {
// Start looping over the query results.
while ( $query->have_posts() ) {
$query->the_post();
echo( get_the_title() );
}
}

Wordpress loop - Display posts by tag from differing post types

I'm trying to display posts within the WP loop and am able to successfully do so with <?php query_posts('tag_id=10'); ?>Here the loop will display all posts with the tag ID of 10, but I'd also like the loop to display posts from within a Custom Post type by the same tag.
I'm able to successfully display posts with tag_id=10 that originate from a custom post type using <?php query_posts('tag_id=10&post_type=videos'); ?>
But how can I merge the two?
I gave this a shot: <?php query_posts('tag_id=10, tag_id=10&post_type=videos'); ?>
but that had no effect.
Any ideas on this one?
You can use this
query_posts(
array(
'post_type' => array('post', 'videos'),
'tag_id' => 10
));
while (have_posts()) : the_post();
// loop code
endwhile;
wp_reset_query();
This runs an action before the posts are actually queried, thus altering the original output to your specific needs.
function tag_archive_mod( $query ) {
if ( is_tag() && $query->is_main_query() ){
$query->set('post_type',array('post','video'));
}
}
add_action('pre_get_posts', 'tag_archive_mod');
Very, very useful.
http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts

Wordpress - related posts by custom taxonomy problem

I'm trying to display related posts based on a custum taxonomy. I found a query at wordpress.org that kind of works. However the original post gets duplicated in the results multiple times. (words is the name of the custom taxonomy I use) What seems to happen is that the single post gets duplicated according to what amount showpost is set. Any idea's what could cause this?
The code:
<?php
//for in the loop, display all "content", regardless of post_type,
//that have the same custom taxonomy (e.g. words) terms as the current post
$backup = $post; // backup the current object
$found_none = '<h2>No related posts found!</h2>';
$taxonomy = 'words';// e.g. post_tag, category, custom taxonomy
$param_type = 'words'; // e.g. tag__in, category__in, but genre__in will NOT work
$post_types = get_post_types( array('public' => true), 'names' );
$tax_args=array('orderby' => 'none');
$tags = wp_get_post_terms( $post->ID , $taxonomy, $tax_args);
if ($tags) {
foreach ($tags as $tag) {
$args=array(
"$param_type" => $tag->slug,
'post__not_in' => array($post->ID),
'post_type' => $post_types,
'showposts'=>5,
'caller_get_posts'=>1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<h3><?php the_title(); ?></h3>
<?php $found_none = '';
endwhile;
}
}
}
if ($found_none) {
echo $found_none;
}
$post = $backup; // copy it back
wp_reset_query(); // to use the original query again
?>
It's inside the foreach loop that you're getting duplications. That code is effectively saying;
Get all the terms for taxonomy type $param_type
For each term, get 5 posts that are tagged with that term
So if you have a post that is tagged with more than one term of the same taxonomy, it's likely it will appear more than once.
You can iteratively add queried posts into the post__not_in array to ensure they don't appear again;
Add $post_not_in = array($post->ID); just above if ($tags) {
Then replace the line post__not_in' => array($post->ID), with post__not_in' => $post_not_in,.
Finally, drop $post_not_in[] = get_the_ID(); inside your while loop, after $found_none = '';
As for me i use this plugin for custom taxonomy relate post. I hope that plugin will help your problem.

Resources