Add text when no result - php

I have to customize a function.
That fonction display custom posts on a (custom) category page.
The problem of this function concerns the absence of text when there is no results.
For instance, on any category page, if there is no available post to show in the grid, I would like to display a text such as "There is no post in this category. Please try another category."
Here is the code:
global $paged, $wp_query, $wp;
$args = wp_parse_args($wp->matched_query);
if ( !empty ( $args['paged'] ) && 0 == $paged ) {
$wp_query->set('paged', $args['paged']);
$paged = $args['paged'];
}
$cat_id = get_queried_object_id();
$temp = $wp_query;
$featuredPosts = array();
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => $classieraFeaturedAdsCounter,
'paged' => $paged,
'cat' => $cat_id,
'meta_query' => array(
array(
'key' => 'featured_post',
'value' => '1',
'compare' => '=='
)
),
);
$wp_query= null;
$wp_query = new WP_Query($args);
while ($wp_query->have_posts()) : $wp_query->the_post();
$featuredPosts[] = $post->ID;
get_template_part( 'templates/classiera-loops/loop-ivy');
endwhile;
wp_reset_postdata();
wp_reset_query();
Does anyone has an idea to include this option?
Thanks.
SOLUTION:
wp_query = new WP_Query($args);
if ( have_posts() ) :
while ($wp_query->have_posts()) : $wp_query->the_post();
$featuredPosts[] = $post->ID;
get_template_part( 'templates/classiera-loops/loop-ivy');
endwhile;
else :
echo wpautop('No result');
endif;

I'm not a WP developer, but it seems to be
if ($wp_query->have_posts()) {
while ($wp_query->have_posts()) : $wp_query->the_post();
} else {
echo 'No post here';
}
Reference link : https://codex.wordpress.org/Function_Reference/have_posts

Related

Not getting paging links when using WP_Query in Wordpress

I am not getting paging links when using WP_Query in WordPress. I am trying to fetch all the products on the page and show them in page by page manner. For this I want to show the paging links on the bottom. But paging links are not showing.
Below is the link - here
And the code for this is:
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'post_type' => 'product',
'posts_per_page' => 3,
'paged' => $paged
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
global $product;
echo '<br />' . woocommerce_get_product_thumbnail().' '.get_the_title().'';
endwhile;
?>
<?php next_posts_link(); ?>
<?php previous_posts_link(); ?>
<? wp_reset_query(); ?>
next_posts_link(); and previous_posts_link(); and not showing links.
Please help.
Use this code instead of of above code.
<?php
$temp = $wp_query;
$wp_query= null;
$postsPerPage = 3;
$argsev = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => $postsPerPage,
'paged' => $paged
);
$wp_query = new WP_Query($argsev);
while ( $wp_query->have_posts() ) : $wp_query->the_post();
global $product;
echo '<br />' . woocommerce_get_product_thumbnail().' '.get_the_title().'';
endwhile; ?>
<?php next_posts_link(); ?>
<?php previous_posts_link(); ?>
<?php
$wp_query = null; $wp_query = $temp;
wp_reset_query();
?>

Wordpress - Exclude posts from author from wp_query

I'm trying to exclude posts by the author with the ID "1" form WP_Query. This still shows all posts by all users.
Any thoughts?
<?php
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query( array( 'author' => -1 ) );
$wp_query->query('posts_per_page=35'.'&paged='.$paged);
while ($wp_query->have_posts()) : $wp_query->the_post();
?>
<div class="grid__third">
...
</div>
<?php endwhile; // end of loop
?>
Thanks a lot!
try this
global $post;
$args=array('author' => '-1', //excludes users with id 1.
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 12,
'ignore_sticky_posts'=> 1,);
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
<li>
<?php the_title(); ?>
</li>
<?php endforeach;
wp_reset_postdata();?>
or
// The Query
$args=array('author' => '-1', //excludes users with id 1.
'post_type' => 'post','post_status' => 'publish','posts_per_page' => 12,'ignore_sticky_posts'=> 1,);
$the_query = new WP_Query( $args ); // 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
}
If your query is not working with
$wp_query = new WP_Query( array( 'author' => -1 ) );
then, You can try some other logic. Like inside the loop get the ID of the author by using POST ID:
$post_author_id = get_post_field( 'post_author', $post_id ); // You will get your author id here
Then apply condition like if($post_author_id != 1){ // YOUR POST } This will display post of all author except author with ID = 1.
Full code like:
while ($wp_query->have_posts()) : $wp_query->the_post();
$post_author_id = get_post_field( 'post_author', HERE YOUR POST ID );
if($post_author_id != 1){
?>
<div class="grid__third">
...
</div>
<?php
} endwhile; // end of loop
Hope this will helpful for you. Thanks.

wordpress pagination not showing

I have custom query on my wordpress page. Query looks like this:
$args = array(
'post_type' => array( 'tworca' ),
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page'=>12,
'post_parent' => 0
);
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args['paged'] = $paged;
// The Query
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
... this is content of my query.....
}
?>
<nav>
<?php previous_posts_link('« Newer') ?>
<?php next_posts_link('Older »') ?>
<?php wp_pagenavi(); ?>
</nav>
<?php
} else {
echo 'no results';
}
/* Restore original Post Data */
wp_reset_postdata();
?>
But my pagination doesn't show. Any idea why? I have read many posts with similar problem, but solution always was to add 'paged' parameter to wp query. And I have this parameter in my query and it doesn't help.
Thanks in advance for your help!
Try This..
`$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'posts_per_page' => 12,
'paged' => $paged,
'orderby' => 'title',
'order' => 'ASC',
'post_parent' => 0,
'post_status' => 'publish',
);
query_posts( $args );
if ( have_posts() ) {
$i = 0;
while ( have_posts() ) {
the_post();
Your data here you want display like title
the_title();
}
wp_pagenavi();
}
wp_reset_query();
?>`
Please try by adding your query object to the wp_pagenavi(), which would be like,
wp_pagenavi( array( 'query' => $the_query ) );
Reference: http://scribu.net/wordpress/wp-pagenavi/wpn-2-74.html
Change wp_pagenavi() to wp_pagenavi( array( 'query' => $the_query) );

using in_category rather than query_posts in wordpress

In wordpress I have a page template called news that I want to display all the posts from one category - 'News'. I don't want to use the category.php because there is a massive blog on the site already.
query_posts('cat=145');
while ( have_posts() ) : the_post();
//do something
endwhile;
Works fine but I have read that query_posts has drawbacks (like speed)
I tried doing this but it just showed me nothing:
while ( have_posts() ) : the_post();
if ( in_category( '145' ) ) : //also tried 'News'
//do something
Why doesn't' in_category work here?
please try this code:
$args = array('post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => 'news' // please pass here you news category slugs
),
)
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
print_r($post);
endwhile;
wp_reset_postdata();
You could WP Query for achieving your requirement.
Documentation: https://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters
Example:
<?php
$args = array(
'cat' => 145,
);
$the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
Try to use get_posts() function:
$get_p_args = array('category'=> 145,'posts_per_page'=>-1);
$myposts = get_posts( $get_p_args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
<div>
<?php the_title(); ?>
</div>
<?php endforeach;
wp_reset_postdata();?>
Try this:
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
global $wp_query;
$args = array_merge( $wp_query->query_vars, array(
'post_type' => 'post', // You can add a custom post type if you like
'paged' => $paged,
'posts_per_page' => 6, // limit of posts
'post_status' => 'publish',
'orderby' => 'publish_date',
'order' => 'DESC',
'lang' => 'en', // use language slug in the query
) );
query_posts( $args );

Having trouble displaying posts from only one category in Wordpress

I'm having difficulty pulling only the desired category into an archive page on Wordpress. My code is below, I thought by defining get-category-by-slug this would work, but instead its pulling all of the post from every category into the page.
<?php
$category = get_category_by_slug('weddings');
$args = array(
'post_type' => 'elp_projects',
'posts_per_page' => 12,
'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1)
);
query_posts($args);
$x = 0;
while (have_posts()) : the_post();
?>
Any ideas on how to fix this would be appreciated.
I've also tried these combinations with no luck.
<?php
$category = get_category_by_slug('weddings');
$args = array(
'post_type' => 'elp_projects',
'posts_per_page' => 12,
'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1)
);
$query = new WP_Query( 'category_name=weddings' );
$x = 0;
while (have_posts()) : the_post();
?>
and
<?php $query = new WP_Query( 'category_name=weddings' ); ?>
and
<?php
$args = array(
'post_type' => 'elp_projects',
'posts_per_page' => 12,
'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1)
);
$query = new WP_Query( 'category_name=weddings' );
$x = 0;
while (have_posts()) : the_post();
?>
Please do not use query_posts its not the best way to query data its a last resort if you will.
Instead use WP_Query to get posts from one category just do $query = new WP_Query( 'category_name=staff' ); refer to this page for more information on how to get posts from one category using WP_Query.
EDITED
Try this
$the_query = new WP_Query( array(
'post_type' => 'page',
'orderby' => 'date',
'category_name' => 'wedding', //name of category by slug
'order' => 'DESC',
'posts_per_page' => )); // how many posts to show
// Put into the loop
while ( $the_query->have_posts() ) :
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
endwhile;
// Restore original Post Data if needed
wp_reset_postdata();

Categories