PHP: Doing while loop if "if" statement is true - php

I am trying to show only those events, which are in current date or in future. To show events I have such code:
<?php
$wp_query = new WP_Query();
global $more;
// set $more to 0 in order to only get the first part of the post
$more = 0;
$wp_query->query( array( 'post_type' => 'events', 'posts_per_page' => 7, 'paged' => $paged, 'orderby' => 'menu_order', 'order' => 'ASC') );
while ( ($wp_query->have_posts()) ): $wp_query->the_post();
get_template_part( 'content', 'event' );
endwhile;
?>
I tried to add "IF statement" to compare event start date with current date, but it doesn't seem to work. I have tried a lots of versions, but this one I think should work:
$wp_query->query( array( 'post_type' => 'events', 'posts_per_page' => 7, 'paged' => $paged, 'orderby' => 'menu_order', 'order' => 'ASC') );
$OstartDate = get_post_meta($post->ID, '_event_start', TRUE);
$today = date('d.m.Y');
while ( ($wp_query->have_posts()) && ($OstarDate < $today) ):
$wp_query->the_post();

From the second one you should try,
$OstartDate=get_post_meta($post->ID,'_event_start',TRUE);//check it will return a number
if(time() > $OstartDate)
{
while ($wp_query->have_posts()):
$wp_query->the_post();
.....
}
a7-simple-events may help you.

You can compare the posts with postmeta using meta_query
$today = date('d.m.Y');
$args=array( 'post_type' => 'events', 'posts_per_page' => 7, 'paged' => $paged, 'orderby' => 'menu_order', 'order' => 'ASC', 'meta_query' => array(
array(
'key' => '_event_start',
'value' => $today,
'type' => 'date',
'compare' => '<'
)
)) ;
$wp_query = new WP_Query( $args );
while ( ($wp_query->have_posts()) ):
$wp_query->the_post();
endwhile;

Related

How do I get posts orderby title with some addtional meta key values

I was searching for this but didn't find the solution. I am getting posts orderby title but some posts need to come first so I added a custom field which meta_key is display_postion and the values for this fields are numbers i.e. 1,2,3. So how do I achieve this, below is my current code.
$args = array(
'post_type' => 'products',
'order' => 'ASC',
'orderby'=> 'title',
'post_status' => 'publish',
'posts_per_page' => -1,
'paged' => $paged
;
$wp_query = new WP_Query( $args );
if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts()) : $wp_query->the_post();
Adding meta_value_num to an orderby array and referencing 'display_position' in the meta_key field should do the trick:
$args = array(
'post_type' => 'products',
'order' => 'ASC',
'orderby' => array(
'meta_value_num' => 'ASC',
'title' => 'ASC'
),
'post_status' => 'publish',
'posts_per_page' => - 1,
'paged' => $paged,
'meta_key' => 'display_position'
);
$wp_query = new WP_Query($args);
if ($wp_query->have_posts()):
while ($wp_query->have_posts()):
$wp_query->the_post();
See documentation for Order and Orderby parameters for WP_Query.

Wordpress custom query: 'orderby' => 'date' not working when using multiple post types

In a Wordpress page template, I set up a WP custom query which queries a custom post type named "recipe" AND the regular posts as below. This works, but 'orderby => 'date' in there doesn't work: The page first lists the regular posts ordered by date, then the recipes by date. But I need ALL of them together (i.e. mixed) ordered by date.
Here's the definition of my custom query:
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$number_of_posts = get_option('posts_per_page', 12);
$args = array(
'post_type' => array('recipe', 'post'),
'post_status' => 'publish',
'posts_per_page' => 12,
'orderby' => 'date',
'order' => 'ASC',
'paged' => $paged
);
$my_loop = new WP_Query($args);
[...followed by the loop...]
I am grateful for any hints what I can do to achieve the desired ordering.
I found a solution myself:
It works when I add remove_all_filters('posts_orderby'); before the custom query is defined. Obviously this resets any other ordering and allows the 'orderby' => 'date' to function as expected. Complete code:
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$number_of_posts = get_option('posts_per_page', 12);
remove_all_filters('posts_orderby'); // ADDED
$args = array(
'post_type' => array('recipe', 'post'),
'post_status' => 'publish',
'posts_per_page' => 12,
'orderby' => 'date',
'order' => 'ASC',
'paged' => $paged
);
$my_loop = new WP_Query($args);
[...]
Q1. what template file are you using?
The query below successfully merges the 2 post types together by date.
<?php
$args = array(
'post_type' => array('my_custom_post_type', 'post'),
'posts_per_page' => -1,
'order' => 'DESC',
'orderby' => 'date',
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post(); ?>
// loop your content here
<?php include(locate_template('templates/content.php')); ?>
<?php
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
?>

Wordpress WP-PageNavi not working with custom query

I have been trying to solve this for a while now but with no success. I have read other similar posts but they are not working for me.
I have created a custom query with displays the correct results and the pagination shows but when i click on the page 2 etc, the url changes accordingly but the same posts remain.
My custom query is:
$sale_properties = new WP_Query(array(
'post_type' => 'properties',
'meta_key' => $nvr_initial.'_price',
'meta_value' => $nvr_price,
'orderby' => 'meta_value_num',
'order' => 'DESC',
'paged' => get_query_var('page'),
'meta_query' => array(
array('key' => $nvr_initial.'_status',
'value' => array('For Sale'),),),));
and my other code is:
<?php /* Display navigation to next/previous pages when applicable */ ?>
<?php if ( $sale_properties->max_num_pages > 1 ) : ?>
<?php if(function_exists('wp_pagenavi')) { ?>
<?php wp_pagenavi( array( 'query' => $sale_properties ) ); ?>
<?php }else{ ?>
<div id="nav-below" class="navigation">
<div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">←</span> Previous', THE_LANG ) ); ?></div>
<div class="nav-next"><?php previous_posts_link( __( 'Next <span class="meta-nav">→</span>', THE_LANG ) ); ?></div>
</div><!-- #nav-below -->
<?php }?>
<?php endif; wp_reset_query();?>
I've tried page, paged and:
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
Could someone please help me out as it's driving me crazy
Kind regards
S
Replace page with paged in below code:
$sale_properties = new WP_Query(array(
'post_type' => 'properties',
'meta_key' => $nvr_initial.'_price',
'meta_value' => $nvr_price,
'orderby' => 'meta_value_num',
'order' => 'DESC',
'paged' => get_query_var('page'),
'meta_query' => array(
array('key' => $nvr_initial.'_status',
'value' => array('For Sale'),),),));
to
$sale_properties = new WP_Query(array(
'post_type' => 'properties',
'meta_key' => $nvr_initial.'_price',
'meta_value' => $nvr_price,
'orderby' => 'meta_value_num',
'order' => 'DESC',
'paged' => get_query_var('paged'),
'meta_query' => array(
array('key' => $nvr_initial.'_status',
'value' => array('For Sale'),),),));
or put
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
above the query and pass $paged variable in query at the place of
get_query_var('page'),
Hopes it will help.
EDIT:
Wordpress standard way:
place on top:
$big = 999999999;
$current_page = get_query_var( 'paged', 1 );
$args = array(
//your query arguments
'paged' => $current_page
);
$my_query = new WP_Query($args);
Use loop like below:
while ( $my_query->have_posts() ) : $my_query->the_post();
// your code
endwhile;
then
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $my_query->max_num_pages
) );
place above where you want to print paging.
I had my custom wp query in the functions.php file. After moving it to my custom page template it worked
$sale_properties = new WP_Query(array(
'post_type' => 'properties',
'meta_key' => $nvr_initial.'_price',
'meta_value' => $nvr_price,
'orderby' => 'meta_value_num',
'order' => 'DESC',
'paged' => get_query_var('paged'),
'meta_query' => array(
array('key' => $nvr_initial.'_status',
'value' => array('For Sale'),),),));

Meta_query in WooCommerce

I'm trying to use the meta_query in WooCommerce product page.
This is the code I'm using:
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' =>8,
'meta_query' => array(
array(
'key' => 'autor',
'value' => '"'.get_the_ID().'"',
'compare' => 'LIKE',
)
),
);
$products = new WP_Query($args);
if ($products->have_posts()) :
$i=0;
while ($products->have_posts()) : $products->the_post();
$autor = get_field('autor');
if($i % 2 ==0) ?>
<h3><?php the_title();?></h3>
<?php if ($i % 2 != 0)
$i++;
endwhile;endif;?>
It doesn't show any title, if I remove the meta_query it shows all products so the problem is that the relation meta_query code is not working. Any ideas how to use it on WooCommerce template?
You use get_the_ID() to get the author id in meta_query args.
get_the_ID() - will get the Post id, not the author id.
To get all posts by authoк id your args should look like this:
$args = array(
'author' => 1,
'post_type' => 'product',
'posts_per_page' => 8,
);
I also see that you using get_field()-function. WordPress core does not have this function. You can use instead get_the_author().
Eventually your code will look like:
<?php
$args = array(
'author' => 1,
'post_type' => 'product',
'posts_per_page' => 8,
);
$products = new WP_Query($args);
if ($products->have_posts()) :
$i=0;
while ($products->have_posts()) : $products->the_post();
$autor = get_the_author();
if($i % 2 ==0) ?>
<h3><?php the_title();?></h3>
<?php if ($i % 2 != 0)
$i++;
endwhile;endif;?>
You can use this code snippet for meta_query in woocommerce
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page'=> 10,
'orderby' => 'total_sales',
'order' => 'DESC',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => '_featured',
'value' => 'yes',
'compare' => '='
),
array(
'key' => 'total_sales',
'value' => '10',
'compare' => '>='
)
)
);
$query = new WP_Query( $args );

Wordpress posts_per_page not working

So, I tried all the "add_filter" things on the whole web to get this right but it doesn't work! It always shows me more than 1 post. What's wrong with my code? Latest WP-Version 4.1.1 and no plugins installed.
Here is the code:
<?php
$sticky = get_option('sticky_posts');
if ( !empty($sticky) ) {
$args = array(
'post__in' => $sticky,
'orderby' => 'date',
'order' => 'ASC',
'posts_per_page' => 1
);
$slider_query = new WP_Query( $args );
while ( $slider_query->have_posts() ) {
$slider_query->the_post();
?>
<div>
<!-- here we go -->
</div>
<?php
}
}
?>
Okay, I got it. This parameter is missing. Wordpress ignores "posts_per_page" by default for sticky posts.
$args = array(
'post__in' => $sticky,
'orderby' => 'date',
'order' => 'ASC',
'posts_per_page' => 1,
'ignore_sticky_posts' => 1
);
Wordpress doesn't play nice with posts_per_page and wp_query.
To fix it you have to use get_query_var, like so:
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
And then in your wp_query() you do:
$args = array(
'post__in' => $sticky,
'orderby' => 'date',
'order' => 'ASC',
'posts_per_page' => 1,
'paged' => $paged
);

Categories