Wordpress Query not Working - wordpress

I am trying to use this code to pull out posts that have the PackageID 3, however it just doesn't seem to work and pulls out any post instead.
What am I missing?
<?php
$args = array(
'orderby' => 'rand',
'order' => 'ASC',
'meta_query' => array(
'key' => 'packageID',
'value' => '3',
'compare' => '=',
'type' => 'NUMERIC',
),
);
query_posts($args); ?>
<?php while (have_posts() ) : the_post(); ?>
<?php the_title(); ?>
<?php endwhile; ?>

I think it's because the meta_query needs to be an array inside an array, so the code would look like
<?php
$args = array(
'orderby' => 'rand',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'packageID',
'value' => '3',
'compare' => '=',
'type' => 'NUMERIC',
)
),
);
query_posts($args); ?>
<?php while (have_posts() ) : the_post(); ?>
<?php endwhile; ?>
https://codex.wordpress.org/Class_Reference/WP_Query

Related

WP How can I combine multipe query?

I like to combine multiple query into one result.
$args_a and $args_b in below code are simplified to summarize my question. I like to combine 2 or more query in one result.
They should not be overwritten each other, so I think "merge" is not proper method to combine this case.
Now, how can I combine them together?
<?php
$args_a = array(
'posts_per_page' => 9,
'paged' => $paged,
'category_name' => 'shinmatsudo',
'category__in' => array( 227 ),
'category__not_in' => array( 3 ),
'meta_query' => array(
'relation' => 'AND',
array(
'key' => '1b',
'compare' => 'NOT EXISTS'
),
array(
'key' => '1d',
'compare' => 'NOT EXISTS'
),
),
);?>
<?php
$args_b = array(
'paged' => $paged,
'posts_per_page' => 9,
'category_name' => 'matsudo',
'category__in' => array( 329 ),
'category__not_in' => array( 3 ),
'meta_query' => array(
'relation' => 'and',
array(
'key'=> '2a',
'value' => array('2020-02-01' , '2020-06-01'),
'compare' => 'BETWEEN',
'type' => 'DATE',
),
),
);
?>
<?php
global $post;
$my_posts= get_posts($args_b);
$my_posts= get_posts($args_a);
foreach($my_posts as $post):setup_postdata($post);?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<h1 class="entry-title"><?php the_title(); ?></h1>
</div>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
The problem your code has is you are overwriting $my_posts variable. Think about what your code is doing:
global $post;
$my_posts= get_posts($args_b);
$my_posts= get_posts($args_a);
You are reassigning $my_posts to the second get_posts so the $args_b will always be overwritten.
You can try using array_merge() to take the results of both get_posts and combine them into a single array.
global $post;
$my_posts= array_merge( get_posts($args_b), get_posts($args_a) );
foreach($my_posts as $post):setup_postdata($post);?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<h1 class="entry-title"><?php the_title(); ?></h1>
</div>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>

How to hide text if query has no posts?

I want to hide the text with the word reviews if query has no posts. Please ask for suggestions.
is image > enter image description here
<h1 class="section-title"><span>Reviews</span></h1>
<?php
$terms = get_the_terms( $post->ID , 'movies', 'string');
$term_ids = wp_list_pluck($terms,'term_id');
$second_query = new WP_Query( array(
'post_type' => 'post',
'cat' => '21',
'tax_query' => array(
array(
'taxonomy' => 'movies',
'field' => 'id',
'terms' => $term_ids,
'operator'=> 'IN' //Or 'AND' or 'NOT IN'
)),
'posts_per_page' => 6,
'orderby' => 'date',
'post__not_in'=>array($post->ID)
) );
//Loop through posts and display...
if($second_query->have_posts()) {
while ($second_query->have_posts() ) : $second_query->the_post();
get_template_part( 'template-parts/content', 'list-01' );
?>
<?php endwhile; wp_reset_query(); }?>
No need of jQuery, just put the h1 tag after the If statement and before while. Then if your query is not empty it will display the title otherwise not
Like this:
<?php
$terms = get_the_terms( $post->ID , 'movies', 'string');
$term_ids = wp_list_pluck($terms,'term_id');
$second_query = new WP_Query( array(
'post_type' => 'post',
'cat' => '21',
'tax_query' => array(
array(
'taxonomy' => 'movies',
'field' => 'id',
'terms' => $term_ids,
'operator'=> 'IN' //Or 'AND' or 'NOT IN'
)),
'posts_per_page' => 6,
'orderby' => 'date',
'post__not_in'=>array($post->ID)
) );
//Loop through posts and display...
if($second_query->have_posts()) { ?>
<h1 class="section-title"><span>Reviews</span></h1>
<?php while ($second_query->have_posts() ) : $second_query->the_post();
get_template_part( 'template-parts/content', 'list-01' );
?>
<?php endwhile; wp_reset_query(); }?>

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.

WordPress Loop - skip posts without a thumbnail

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
)
)
);

Resources