WordPress Loop - skip posts without a thumbnail - wordpress

I want skip every post that has no thumbnail. The code does not work properly yet.
Actually the script doesn't show posts without a thumbnail - that's good, but in the loop the post with no thumbnail is still counted as a post.
So when i have for example 10 posts in my wordpress database. I want show 5 of them. But only the posts who has a thumbnail.
<ul>
<?php
$args = array( 'numberposts' => 5,
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish'
);
$my_posts = get_posts( $args );
global $post;
foreach( $my_posts as $post ) : setup_postdata($post);
if ( !has_post_thumbnail() ) {
continue;
} else {
?>
<li>
<div class="clearfix" >
<div class="thumb"><?php the_post_thumbnail('post-image-big'); ?></div>
<?php the_title(); ?>
<p class="category"><?php the_category(', '); ?></p>
</div>
</li>
<?php } ?>
<?php endforeach; ?>
</ul>

Try
$args = array( 'numberposts' => 5,
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish' ,
'meta_query' => array(
array(
'key' => '_thumbnail_id',
'compare' => '!=',
'value' => ''
)
)
);
or this if checking for an empty string didn't work for you
$args = array( 'numberposts' => 5,
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish' ,
'meta_query' => array(
array(
'key' => '_thumbnail_id',
'compare' => '!=',
'value' => null
)
)
);

Related

How do i show only 1 post from latest 10 post?

my english language is not good. sorry!
this code is for checkbox in advanced custom field plugin.
i want show only 1 post(randomly) from latest 10 post.
please help me. thanks
('posts_per_page' => 10) and ('numberposts' => 10) is not working.
<?php
$gallery = array(
"offset" => "0",
'showposts' => '1',
'orderby' => 'rand',
'meta_query' => array(
array(
'key' => 'postcat',
'value' => '"selection"',
'compare' => 'LIKE'
)));
// query
$qgallery = new WP_Query( $gallery );
?>
<?php if( $qgallery->have_posts() ): ?>
<?php while( $qgallery->have_posts() ) : $qgallery->the_post(); ?>
<div class="fromgallery">
<a href="0" class="frgall">
<span class="frgdesc"><?php the_title() ?></span>
</a></div>
<?php endwhile; ?><?php endif; ?>
Try this
$gallery = array(
'post_type' => 'post',
'posts_per_page' => 10,
'order' => 'DESC',
'no_found_rows' => 'true',
'_shuffle_and_pick' => 1
'meta_query' => array(
array(
'key' => 'postcat',
'value' => '"selection"',
'compare' => 'LIKE'
)));
$qgallery = new \WP_Query( $gallery );
I only do change in your code. For randomly one post you need to use '_shuffle_and_pick' => 1 , 'posts_per_page' => 10 is from 10 post and 'order' => 'DESC' is for latest posts. for custom '_shuffle_and_pick' you need to add
add_filter( 'the_posts', function( $posts, \WP_Query $qgallery )
{
if( $pick = $qgallery->get( '_shuffle_and_pick' ) )
{
shuffle( $posts );
$posts = array_slice( $posts, 0, (int) $pick );
}
return $posts;
}, 10, 2 );
To show Random post , Please use below script:
$args = array(
'post_type' => 'post',
'posts_per_page' => 10,
'orderby' => 'date',
'order' => 'DESC',
'no_found_rows' => 'true',
'_shuffle_and_pick' => 1
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'postcat',
'value' => '"selection"',
'compare' => 'LIKE'
)
)
);
$the_query = new WP_Query( $args );
//check is post found
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo get_the_title();
}
wp_reset_postdata();
} else {
echo 'no posts found';
}
I hope it will help you :)

Prioritizing wp_query by meta key

I have two custom fields for views. weekly_views and all_views. The weekly views custom field is deleted every week and starts counting views again from 0. So now what I want to achieve is show 12 posts by weekly views but when the custom field is deleted and unless there are views on those posts the query shows nothing. I want to show here posts by all_views instead of no posts.
My query goes as follows but it's not working as I want. In short what I want to achieve is to show posts by weekly_views custom field but if there's no post then show posts by all_views. And also if there's less than 12 posts by weekly_views then show weekly_views posts first and then remaining posts by all_views.
$args = array(
'post_type' => array( 'custom_post_type_1', 'custom_post_type_2'),
'posts_per_page' => '12',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'weekly_views',
),
array(
'key' => 'all_views',
),
),
);
The above code is returning me posts but are sorted by all_views.
Edit
The new query that's working for me
<?php
$args = array(
'post_type'=> array( 'custom_post_type1', 'custom_post_type2'),
'posts_per_page' => '12',
'meta_key' => 'weekly_views',
'orderby' => 'meta_value_num',
'order' => 'DESC',
);
$the_query = new WP_Query( $args );
if ($the_query->post_count < 12) {
$countweeklyposts = $the_query->post_count;
$showallpostscount = 12 - $countweeklyposts;
$args2 = array(
'post_type'=> array( 'band', 'artist'),
'posts_per_page' => $showallpostscount,
'meta_key' => 'all_views',
'orderby' => 'meta_value_num',
'order' => 'DESC',
);
$the_query2 = new WP_Query( $args2 );
}
?>
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
//Code to show posts goes here
<?php
endwhile;
wp_reset_postdata();
?>
<?php while ($the_query2 -> have_posts()) : $the_query2 -> the_post(); ?>
//Code to show posts goes here
<?php
endwhile;
wp_reset_postdata();
?>
You could do this too if you want a little less code
<?php
$args = array(
'post_type'=> array( 'custom_post_type1', 'custom_post_type2'),
'posts_per_page' => '12',
'meta_key' => 'weekly_views',
'orderby' => 'meta_value_num',
'order' => 'DESC',
);
$args2 = array(
'post_type'=> array( 'band', 'artist'),
'posts_per_page' => '12',
'meta_key' => 'all_views',
'orderby' => 'meta_value_num',
'order' => 'DESC',
);
if ($query->post_count > 12) {
$query_args = $args;
}else if($query->post_count < 12){
$query_args = $args2;
}
$query = new WP_Query( $query_args );
while ($query -> have_posts()) : $query -> the_post();
//Code to show posts goes here
endwhile;
wp_reset_postdata();
?>

Wordpress static front-page made up of child pages

Is there a way to make a static front-page made up of child pages, surrounded by sections, like this:
Front Page
<div class="main">
Parent Start
<section id="<section title>">
Child Content
</section>
<section id="<section title>">
Child Content
</section>
<section id="<section title>">
Child Content
</section>
Parent End
</div>
I was thinking the section id could be added from the menu settings?
Appreciate if anybody could point me in the right direction!
SOLUTION
$args = array(
'posts_per_page' => -1,
'meta_key' => 'priority',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'post_type' => 'page',
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'add_to_front_page',
'value' => 'Yes',
'compare' => '=',
),
),
);
$pages = get_posts( $args );
foreach ( $pages as $page ) {
$title = $page->post_title;
$content = wpautop( $page->post_content );
}
priority and add_to_front_page are custom fields!
Use WP_Query.
<div class="main">
<?php
$args = array(
'posts_per_page' => -1,
'meta_key' => 'priority',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'post_type' => 'page',
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'add_to_front_page',
'value' => 'Yes',
'compare' => '=',
),
),
);
$query = new WP_Query( $args );
while($query->have_posts() ):
$query->the_post() : ?>
<section id="<section title>">
<?php the_title();
the_content(); ?>
</section>
<?php
endwhile;
wp_reset_postdata();
?>
</div>

Wordpress show future events based on custom field

I know similar questions are asked a million times and i have tried different kinds of solutions but without any success
I have Cpt contests
<?php
$paged = ( get_query_var('paged') ) ?get_query_var('paged') : 1;
$contest = new WP_Query(
array(
'post_type' => 'contests',
'posts_per_page' => '15',
'meta_key'=> '_closingdate',
'orderby'=> 'meta_value',
'order' => 'ASC',
'paged' => $paged
));
?>
<?php if ($contest->have_posts()) : while ($contest->have_posts()) : $contest->the_post();?>
<div class="row">
<div class="cell_left"><p><a title="<?php the_title(); ?>" href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></p></div>
<div class="cell"><p class="posted"><?php echo date('d-m-Y', strtotime(get_post_meta($post->ID, "_closingdate",true)));?></p></div>
</div>
<?php endwhile;?>
</div>
<div class="navigation">
<?php wp_pagenavi( array( 'query' => $contest ) ); ?>
<?php wp_reset_query();?>
</div>
<?php endif; ?>
This is working without any problem
now i try to only show "contests" with closing date today and future.
i have found this on stack overflow but i cant get it working .
<?php
$paged = ( get_query_var('paged') ) ?get_query_var('paged') : 1;
$today = date('Ymd');
$contest = new WP_Query(array(
'post_type' => 'contests',
'posts_per_page' => '15',
'meta_key' => '_closingdate',
'orderby' => 'meta_value',
'paged' => $paged,
'order' => 'ASC',
'meta_query' => array(
array(
'key' => '_closingdate',
'meta-value' => $value,
'value' => $today,
'compare' => '>=',
'type' => 'CHAR',
)
)
));
?>
Is there someone who can solve this quest for me ?
Cheers
try using php time function:
in your case
<?php
$paged = ( get_query_var('paged') ) ?get_query_var('paged') : 1;
$contest = new WP_Query(array(
'post_type' => 'contests',
'posts_per_page' => '15',
'meta_key' => '_closingdate',
'orderby' => 'meta_value',
'paged' => $paged,
'order' => 'ASC',
'meta_query' => array(
array(
'key' => '_closingdate',
'value' => time(),
'compare' => '>=',
)
)
));
?>

How do I show most viewed posts on a per-month basis?

For last month this query worked, now this 3 days (new month started), no posts at all:
<?php $current_year = date('Y'); ?>
<?php $current_month = date('m'); ?>
<?php endif;
arras_featured_loop( arras_get_option('featured1_display'),
apply_filters('arras_featured1_query', array(
'list' => $featured1_cat,
'taxonomy' => arras_get_option('featured1_tax'),
'query' => array( 'posts_per_page' => 15,
'meta_key' => 'post_views_count',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'year' => $current_year,
'monthnum' => $current_month,
'category_name' => Music,
'posts_per_page' => $featured1_count,
'exclude' => $post_blacklist,
'post_type' => arras_get_option('featured1_posttype')
)
) ) );
?>
Any kind of help will be appreciated.
UPDATE:
This was not pissible.
But now I found way to do this with plugin Wordpres Popular Posts.
Its working alone in template with this code:
<?php
if (function_exists( 'wpp_get_mostpopular' )) {
wpp_get_mostpopular('range=weekly&cat=276&order_by=views&limit=8');
}
?>
Now, what I need to do is to use this code up into this template down:
<?php endif;
arras_featured_loop( arras_get_option('featured1_display'), apply_filters('arras_featured1_query', array(
'list' => $featured1_cat,
'taxonomy' => arras_get_option('featured1_tax'),
'query' => array(
'posts_per_page' => $featured1_count,
'exclude' => $post_blacklist,
'post_type' => arras_get_option('featured1_posttype')
)
) ) );
?>
I think I need to put it inside here:
'query' => array(
Problem is I don't know to turn query from existing to one with arrows 'range=weekly' to 'range' => weekly didnt worked.

Resources