Wordpress show future events based on custom field - wordpress

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' => '>=',
)
)
));
?>

Related

Order Wordpress custom posts by custom feilds

I am trying to order custom posts by custom fields. this is my code.
$paged = get_query_var('paged') ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'pql_Servay',
'paged' => $paged,
'post_status' => 'publish',
'posts_per_page' => 2,
'orderby' => 'meta_value',
'orderby' => 'meta_value_num',
'meta_key' => 'question_order',
'order' => 'ASC'
);
This code works for displaying all posts on a single page. But I want to display one post per page.
I am using this code to get next post page link.
Next Post
But When I click this link next page also shows the first post. In short next post is displaying the same post on the first page.
I am creating a shortcode to display these posts.
Here is complete shortcode
<?php
add_shortcode( 'PreQualifyingLeads', 'PreQualifyingLeads_shortcode');
function PreQualifyingLeads_shortcode(){
$paged = get_query_var('paged') ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'pql_Servay',
'paged' => $paged,
'post_status' => 'publish',
'posts_per_page' => 2,
'orderby' => 'meta_value',
'orderby' => 'meta_value_num',
'meta_key' => 'question_order',
'order' => 'ASC'
);
?>
<div class="pql-wrapper">
<div class="title-wrapper">
<h2>This is page Title </h2>
</div>
<div class="Question-Wrapper">
<?php
$loop = new WP_Query( $args );
if($loop->have_posts()){
while ( $loop->have_posts() ) {
$loop->the_post();
echo the_title( ).'<br>';
}
}
wp_reset_postdata( );
?>
Next Post
</div>
</div>
<?php
}
You need to add paged parameter in your query like following:
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
'paged' => $paged
Your wp query should look like the following:
$paged = get_query_var('paged') ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'pql_Servay',
'paged' => $paged
'post_status' => 'publish',
'posts_per_page' => 1,
'orderby' => 'meta_value',
'orderby' => 'meta_value_num',
'meta_key' => 'question_order',
'order' => 'ASC'
);
Try this may be its run
$args = array(
'post_type' => 'pql_Servay',
'paged' => $paged
'post_status' => 'publish',
'posts_per_page' => 1,
'orderby' => array(
'question_order'=>'ASC
)
);

Wordpress Query not Working

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

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>

Sort Events By Event Date, Not Post Date

Here's the page I'm working on.
This is what I was trying, but it won't validate.
$today = date('Ymd');
$args = array(
'post_type' => 'events',
'posts_per_page' => 10,
'meta_key' => 'calendar_date', // name of custom field
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'calendar_date',
'compare' => '<=',
'value' => $today,
)
),
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
Do you use this code in event page?
<?php
$today = date('m/d/Y');
$args = array(
'post_type' => 'events',
'posts_per_page' => 10,
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_key' => 'calendar_date', // name of custom field
'meta_compare' => '<=',
'meta_value' => $today
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
$convert = strtotime( get_field('calendar_date') );
$calendarDate = date('M d', $convert);
?>
<div class="news-date">
<p><?php echo $calendarDate; ?></p>
<h2><?php the_title(); ?></h2>
<i class="fa fa-2x fa-angle-right"></i>
</div>
<?php } ?>
<?php } ?>

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