Display div only if exists other posts with same custom field value - php

I've got a wordpress theme with a set of custom fields.
One of these is named "author".
On single.php I've got a div which show the other posts with the same custom field value.
I would like to display this div only if exists other posts with the same custom field value, else I would like to display nothing.
Thanks for your help!!
This is my actual code:
<?php
$myquery = array(
'meta_key' => 'autore',
'meta_value' => $autore,
'showposts' => 2,
'post__not_in' => array($post->ID)
);
if ( $myquery->have_posts() ) : ?>
<div class="related">
<h3>Altre di <?php the_field('autore'); ?></h3>
<ul>
<?php while ( $your_query->have_posts() ) : $your_query->the_post(); ?>
<?php
echo '<li>'; ?>
<?php
$fotorel = get_field('foto_homepage');
list($width, $height) = getimagesize("$fotorel");
$relheight = $height / 2;
?>
<div class="related-foto" style="background:url(<?php the_field('foto_homepage'); ?>) no-repeat center center; height:<?php echo $relheight.'px' ?>"></div>
<?php the_title(); ?>
<?php echo '</li>';?>
<?php endwhile; ?>
<?php else : // What to do if there are no posts from that author
endif;?>
</ul>
</div>
<?php wp_reset_query(); ?>

Here an example:
http://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query
Using the condition <?php if ($pageposts): ?> you can print your div or not.

I'm not sure how you are querying for the posts in the custom fields but the $wp_query has built in conditionals for handling queries that don't return posts.
Updated code sample:
$args = array(
'meta_key' => 'autore',
'meta_value' => $autore,
'showposts' => 2,
'post__not_in' => array($post->ID)
);
$your_query = new WP_Query( $args );
if ( $your_query->have_posts() ) : ?>
<div id="your-div">
while ( $your_query->have_posts() ) : $your_query->the_post();
// Do stuff
endwhile;
else : // What to do if there are no posts from that author
endif;

Related

Show recent blog posts, but exclude current post

I want to display the 3 most recent posts at the bottom of my single-blog page, but without the current posts.
my code:
<div class="blog__posts">
<h2><?php esc_html_e('andere blogberichten', 'dfib-theme'); ?></h2>
<?php esc_html_e('alle blogberichten', 'dfib-theme'); ?>
<div class="blog__posts--wrapper">
<?php
$the_query = new WP_Query( array(
'posts_per_page' => 2,
));
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="blog__single">
<div class="blog__single--img" style="background-image: url(<?php echo get_the_post_thumbnail_url();?>);"></div>
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
lees meer
</div>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php __('No News'); ?></p>
<?php endif; ?>
</div>
</div>
It displays 3 posts, but when I visit the most recent post, the same post is displayed at the bottom again. Is there a way to exclude the current one every time?
I just found the solution on this website: https://pineco.de/snippets/exclude-current-post-from-wp_query/
I just added this piece to my query:
'post__not_in' => array(get_the_ID())
You have to exclude the current post with the post__not_in.
Add post__not_in in WP_Query array like below.
<?php
$the_query = new WP_Query( array(
'posts_per_page' => 2,
'post__not_in' => array( get_the_ID() )
));
?>

How filter custom posts by Advanced Custom Fields checkbox

I am creating a property site where there is a featured property on the home page. To define a property as featured I have created an acf checkbox with the value as Yes when checked. I have tried filtering the posts by checking if the checkbox is checked but I cannot figure it out. Here's my code which isn't working;
<?php
$args = array(
'post_type' => 'property',
'posts_per_page' => 1,
'meta_key' => 'featured_property',
'meta_value' => 'Yes'
);
$query = new WP_Query( $args );
?>
<?php if( $query->have_posts() ) : ?>
<?php
$main_field = get_field('images');
$first_row = $main_field[0];
$img = $first_row['image'];
$img_crop = $img['sizes']['fresh_size'];
?>
<img src="<?php echo $img_crop; ?>" alt="featuredproperty" class="img-fluid">
<?php wp_reset_postdata(); ?>
<?php endif; ?>
READ THIS: for anyone attempting to do this with a checkbox like I was don't. after a little research i found out "Checkboxes are stored as serialized data and you’re not going to be able to use WP_Query to filter by a checkbox field" Use true / false instead and check if the value equals '1' or '2' depending on what you are trying to achieve.
https://support.advancedcustomfields.com/forums/topic/using-checkbox-fields-in-custom-queries/
Remove this part:
'meta_key' => 'featured_property',
'meta_value' => 'Yes'
Instead, filter out who has the checkbox checked inside the loop. You are also missing parts of the loop. Try this code:
<?php if( $query->have_posts() ) : ?>
(...)
<!-- start of the loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php if( get_field('featured_property') ) { // << FROM HERE ?>
<img src="<?php echo $img_crop; ?>" alt="featuredproperty" class="img-fluid">
<?php } // << TO HERE ?>
<?php endwhile; ?><!-- end of the loop -->
<?php wp_reset_postdata(); ?>
<?php endif; ?>
I have cut out the first part of your code to make it easier to read.
--
Or, if you would like to use meta_key instead, try adding:
'compare' => 'EXISTS'
<?php
$args = array(
'post_type' => 'property',
'posts_per_page' => 1,
'meta_key' => 'featured_property',
'meta_value' => 'Yes'
);
$query = new WP_Query( $args );
?>
<?php if( $query->have_posts() ): ?>
<ul>
<?php while( $query->have_posts() ) : $query->the_post();
$images = get_field('images');
$first_row = $main_field[0];
$img = $first_row['image'];
$img_crop = $img['sizes']['fresh_size'];
?>
<img src="<?php echo $img_crop; ?>" alt="featuredproperty" class="img-fluid">
<?php endwhile; ?>
</ul>
<?php endif; ?>
<?php wp_reset_query(); // Restore global post data stomped by the_post().
?>
Please check your settings under acf checkbox "Choices", check if it "Yes : Yes" or "yes : Yes" and fix your 'meta_value' if you have "yes : Yes" to 'meta_value' => 'yes'. Checkbox saves data as value-label. I think you have issue with your configuration of checkbox.
What type of 'images' field do you use? Is it repeater or gallery? If you using gallery then for src of the image you need to use:
$images = get_field('images');
$img_crop = $images[0]['sizes']['fresh_size'];

Why is my Wordpress Post Loop not working?

Okay, I am trying run the wordpress post loop to print out the post titles and content using the_title() and the_content(). But It is not printing out the posts on the page?
Any ideas?
index.php
<div class="blog-container">
<!-- The POST LOOP -->
<?php
if(have_posts()) :
while (have_posts()) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
<?php endwhile; ?>
<?php else :
echo '<p>No content found</p>';
endif;
?>
</div>
EDIT Okay, I'm getting ONLY THE content printed out not the post titles, here is an image: http://imgur.com/a/DiBqG.
EDIT 2 Inside wordpress admin post should look like: http://imgur.com/a/1DQAp
You don't define the query and use the Wordpress build in functions in the wrong way. Here is the correction.
<?php
// The Query
$the_query = new WP_Query( );
// 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>';
/* Restore original Post Data */
wp_reset_postdata();
} else {
// no posts found
}
add below code for fetch post data
<?php
$post_args=array(
'type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'date',
'order' => 'ASC',
);
$post_my_query = null;
$post_my_query = new WP_Query($post_args);
if( $post_my_query->have_posts() )
{
while ($post_my_query->have_posts()) : $post_my_query->the_post();
?>
<h2><?php echo get_the_title( $post_my_query->ID );?></h2>
<?php
echo get_the_content( $post_my_query->ID );
endwhile;
}
wp_reset_query($post_my_query);
?>

Display all posts in category wordpress

I am trying to display all posts in the category on the category archive page.
I have used the following code, but it displays all posts from all categories.
Can someone help me please?
<?php
// the query
$wpb_all_query = new WP_Query(array('post_type'=>'post', 'post_status'=>'publish', 'posts_per_page'=>-1)); ?>
<?php if ( $wpb_all_query->have_posts() ) : ?>
<ul>
<!-- the loop -->
<?php while ( $wpb_all_query->have_posts() ) : $wpb_all_query->the_post(); ?>
<li><?php the_title(); ?></li>
<?php endwhile; ?>
<!-- end of the loop -->
</ul>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
Thanks
You need to add a category parameter to the WP_Query array.
Check this reference in the "Category Parameters" section: https://codex.wordpress.org/Class_Reference/WP_Query
Something like this should work:
$wpb_all_query = new WP_Query(array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'category_name' => 'your_category_slug'
));

Showing only part of the custom meta in wordpress

I have a wordpress code that displays a check list for each wordpress user. The code works fine and I am able to get it to display on a page.
<?php
$args = array( 'post_type' => 'todo_listing', 'posts_per_page' => 3,'order'=>'ASC' );
$loop = new WP_Query( $args );
$_meta_val_arr=array(10=>"All Item",0=>"Cat0",
1=> "cat1",
2=>"cat2",
);
?>
<?php
while ( $loop->have_posts() ) : $loop->the_post();
?>
<li class="todo" id="<?php echo get_the_ID();?>" itemage="<?php echo get_post_meta(get_the_ID(),'_todotime',true)?>"><a href="javascript:;"
<?php if($all_meta_for_user[get_the_ID()][0]){
?>
class="strike"
<?php
}
?>
>
<?php if($all_meta_for_user[get_the_ID()][0]){?>
<span class="check_box cb"></span>
<?php }else{?>
<span class="uncheck_box cb"></span>
<?php }?>
<p><?php the_title(); ?></p></a>
<?php
endwhile;
?>
I just want to show those items with with Class ="strike" which is saved as meta [1012] => Array ( [0] => 1 ) where 1012 is the todo id.

Categories