I am running a custom query but when returning posts_per_page, it is always two behind. I am using a custom post type called events, that is pulling in the code to display the posts from the functions file. I also want to implement pagination(not working also), will this be a problem?
I have ran var dump which returns everything in the query including posts before the current date. I imagine because it's dumping before the if statement? Any ideas?
Any ideas?
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
//Current exhibitions
$args = array(
'post_type' => 'exhibitions',
'orderby' => 'exhibition_start_date',
'meta_key' => 'exhibition_start_date',
'order' => 'ASC',
'posts_per_page' => 6,
'paged' => $paged
);
$my_query = new WP_Query( $args );
while ( $my_query->have_posts() ) : $my_query->the_post();
$exhibition_start_date = get_post_meta($post->ID, 'exhibition_start_date', true);
//Current
if ($exhibition_start_date >= date('Y-m-d') ) {
get_exhibition_container();
}
endwhile;
wp_reset_postdata();
?>
New code: (I've also added in my initial loop which brings in the page content, thought it could interfear?)
<div role="main" class="clearfix">
<div class="one-column">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
<?php endwhile; else: ?>
<?php endif; ?>
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
//Current exhibitions
$args = array(
'post_type' => 'exhibitions',
'meta_query' => array(
array (
'key' => 'exhibition_start_date',
'value' => date('Y-m-d'),
'compare' => '>='
)
),
'orderby' => 'exhibition_start_date',
'meta_key' => 'exhibition_start_date',
'order' => 'ASC',
'posts_per_page' => 10,
'paged' => $paged
);
$my_query = new WP_Query( $args );
while ( $my_query->have_posts() ) : $my_query->the_post();
$exhibition_start_date = get_post_meta($post->ID, 'exhibition_start_date', true);
//Current
get_exhibition_container();
endwhile;
wp_reset_postdata();
?>
<div class="previous-holder">
<?php previous_posts_link(); ?>
</div>
<div class="next-holder">
<?php next_posts_link(); ?>
</div>
Instead of using a if condition in your loop, you should use meta_query param :
$args = array(
'post_type' => 'exhibitions',
'meta_query' => array(
array(
'key' => 'exhibition_start_date',
'value' => date('Y-m-d'),
'compare' => '>='
)
),
'orderby' => 'exhibition_start_date',
'meta_key' => 'exhibition_start_date',
'order' => 'ASC',
'posts_per_page' => 6,
'paged' => $paged
);
EDIT : About pagination
previous_posts_link codex :
Prints a link to the previous set of posts within the current query.
You should try using previous_posts_link() and next_posts_link() before wp_reset_postdata()
Related
So basically I have some posts on a post page. The posts show and the pagination shows. However, when you click the next page it does not go to the next page.. it cleans the URL and just reloads the same page. Not sure if there is a different way to be doing this? Any advice here would be great!
Code:
<div class="list-grid-view">
<?php get_template_part( 'template-parts/episode-filter' ); ?>
<div class="filtered-posts">
<?php
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
global $wp_query;
$original_query = $wp_query;
$args = array(
'post_type' => 'episode',
'posts_per_page' => 8,
'paged' => $paged,
'post_status' => 'publish',
'orderby' => 'date',
'order' => 'DESC'
);
$the_query = new WP_Query( $args );
$wp_query = $the_query;
if ( $the_query->have_posts()) :
while ( $the_query->have_posts() ) : $the_query->the_post();
get_template_part( 'template-parts/episode-content-post' );
endwhile;
the_posts_pagination(array(
'mid_size' => 2,
'prev_text' => __( '<', 'textdomain' ),
'next_text' => __( '>', 'textdomain' ),
));
endif;
$wp_query = $original_query;
?>
</div>
</div>
I have custom page for posts which has to show up 9 posts per page and I used wp_pagenavi plugin but it doesn't work. Please help!
<div class="page-content__wrapper">
<?php
$post_category = get_field('page_category');
$posts = get_posts( array(
'numberposts' => 9,
'category_name' => $post_category,
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'post',
'suppress_filters' => true,
) );
foreach( $posts as $post ){
setup_postdata($post);
?>
//...posts
<?php
}
wp_pagenavi();
wp_reset_postdata();
?>
</div>
I used WP_Query() to solve this problem (without wp_pagenavy plugin :)
<?php
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1; // setup
$post_category = get_field('page_category'); // getting category name
$posts= new WP_Query(array(
'post_type'=> 'post',
'posts_per_page' => 9,
'paged' => $paged, // don't forget to give this argument
'category_name' => $post_category,
));
if($posts->have_posts()) :
while($posts->have_posts()) : $posts->the_post(); ?>
// All posts will be here!
<?php endwhile; ?>
<div class="mt-30 text-center">
<?php
$GLOBALS['wp_query'] = $posts;
the_posts_pagination(
array(
'mid_size' => '2',
'prev_text' => '<i class="fa fa-hand-o-left"></i> <',
'next_text' => '> <i class="fa fa-hand-o-right"></i>',
'screen_reader_text' => ' '));
?>
</div>
<?php else :?>
<h3><?php _e('404 Error: Not Found'); ?></h3>
<?php endif; ?>
<?php wp_reset_postdata();?>
Trying to get all the posts having the same zipcode as metavalue. Thanks in advance for the help.
<?php
$query = new WP_Query( array(
'post_type'=> array('service'),
'posts_per_page' => -1,
'meta_query' => array( array(
'key'=> 'zipcode',
'value'=> ','.$zip.',',
'compare'=> 'LIKE'
) )
));
?>
<?php if ( $query->have_posts() ) :while ( $query->have_posts() ) : $query->the_post(); ?>
<h3><?php the_title(); ?></h3>
<?php endwhile; // end of the loop. ?>
<?php wp_reset_query(); ?>
<?php else: ?>
No results found.
<?php endif; ?>
zipcode are numbers for example 12345. If posts have value 12345 in the custom field. then it should display all posts which have the 12345 value. The above code is working fine but displays only one post.
Following code will be the proper for the meta query.
$query_args = array(
'post_type' => 'service',
'posts_per_page' => -1,
'meta_query' => array(
array(
'value' => $zip,
'compare' => 'LIKE',
'key' => 'zipcode',
),
)
);
$query = new WP_Query($query_args);
<?php if ( $query->have_posts() ) :while ( $query->have_posts() ) : $query->the_post(); ?>
<h3><?php the_title(); ?></h3>
<?php endwhile; // end of the loop. ?>
<?php wp_reset_query(); ?>
<?php else: ?>
No results found.
<?php endif; ?>
Hope it helps.
This does the job for me:
$popularCourses = new WP_Query(
array(
'post_type' => 'courses',
'posts_per_page' => -1,
'post_status' => 'publish',
'meta_key' => 'course-is-promoted',
'meta_value' => 'Yes',
'orderby' => 'date',
'order' => 'DESC'
)
);
There are two ways.
After watching this code i will suggest you just visit this link for your better understanding.
(1)
$args = array(
'meta_query' => array(
array(
'key' => 'Your_key',//Enter your meta key here
'value' => 'professionnel',//Enter you meta value
'compare' => '=',//Comparison type (option filed) .
)
)
);
$query = new WP_Query($args);
(2)
$output_loop = get_posts( array(
'meta_key' => 'Your_key',//Meta key
'meta_value' => 'Your_value',//Meta value
) );
Now just print_r($output_loop) for the better understanding.
I have a taxonomy called portfolio_cat with its categories.So now i need to create a slider with that categories as a title and their post items.How i can do that? What Wordpress loop i need to have so i could put in a slider Wordpress categories with their posts?
I dont know how to customize this loop to fit in
<?php
$query = new WP_Query( array('post_type' => 'portfolio', 'posts_per_page' => 7, 'order' => ASC ) );
while ( $query->have_posts() ) : $query->the_post();
?>
I think you need this:
$portfolioArgs = array(
'order' => 'ASC',
'post_type' => 'portfolio',
'posts_per_page' => 7,
'tax_query' => array(
array(
'taxonomy' => 'portfolio_cat',
'field' => 'slug'
)
)
);
$query = new WP_Query( $portfolioArgs );
while ( $query->have_posts() ) : $query->the_post(); ?>
<div class="slide">
<?php the_title( '<h2>', '</h2>' ); ?>
<?php the_excerpt(); ?>
</div>
<?php
endwhile;
?>
I have some certain categories' ids. I want to loop this categories and last 3 posts in one time. I try this but only come one category from array.
<?php
$args = array(
'cat' => 48,43,49,46,47,44,51,50,42,
'order' => 'ASC',
'showposts' => 3
);
query_posts($args);
?>
<?php while (have_posts()) : the_post(); ?>
<?php the_title(); ?>
<?php endwhile; ?>
This piece of code won't work: 'cat' => 48,43,49,46,47,44,51,50,42,
You'll have to use an array 'cat' => array(48,43,49,46,47,44,51,50,42),
For some reason 'cat' did not work. We used
'category__in' => array( 2, 6 ),
and it workined fine.
The completed working code:
<?php
// -----------------------------
$args = array(
'post_type' => 'post',
'order' => 'ASC',
'category__in' => array(2,6)
);
$query = new WP_Query( $args );
?>
You can get All Posts in a Category which one you want publish.
query_posts( array ( 'category_name' => 'my-category-slug', 'posts_per_page' => -1 ) );
You can find post as per your expectation by.
query_posts( array ( 'category_name' => 'carousel', 'posts_per_page' => 10, 'orderby' => 'date', 'order' => 'DESC' ) );
According to your code. Update --
<?php
$args = array(
'cat' => [48,43,49,46,47,44,51,50,42], //change here array
'order' => 'ASC',
'posts_per_page' => 3 //showposts deprecated now
);
query_posts($args);
?>
<?php while (have_posts()) : the_post(); ?>
<?php the_title(); ?>
<?php endwhile; ?>
<?php wp_reset_query(); ?> // you should reset your query
should actually be:
'cat' => '48,43,49,46,47,44,51,50,42'