Order Wordpress custom posts by custom feilds - wordpress

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

Related

When sorting posts by post meta in WP_Query, only posts with a meta value are returned

I need to sort posts by rating. And this code does the job well:
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array (
'post_type' => 'brands',
'posts_per_page' => 6,
'meta_key' => 'post_average_rating',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'paged' => $paged
);
$brands = new WP_Query( $args );
?>
But there is a problem. If a post does not have a rating value, it simply does not appear. As you can see, I need six posts to be displayed on the page. But if all posts have a rating in only three, only three will be displayed. And I would like to display post without a rating also. Please tell me how this can be solved?
thanks FluffyKitten's comment the solution was found. Here is the code:
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array (
'post_type' => 'brands',
'posts_per_page' => 6,
'orderby' => 'meta_value_num',
'order' => 'DESC',
'meta_query' => array(
'relation' => 'OR',
array(
'key'=>'post_average_rating',
'compare' => 'EXISTS'
),
array(
'key'=>'post_average_rating',
'compare' => 'NOT EXISTS'
)
),
'paged' => $paged
);
$brands = new WP_Query( $args );
?>
Posts with a rating are now displayed first, sorted from highest to lowest, and posts that have no rating are just displayed after them.
Reference: Orderby meta value only returns posts that have existing meta key on wordpress.stackexchange.com

Prioritizing wp_query by meta key

I have two custom fields for views. weekly_views and all_views. The weekly views custom field is deleted every week and starts counting views again from 0. So now what I want to achieve is show 12 posts by weekly views but when the custom field is deleted and unless there are views on those posts the query shows nothing. I want to show here posts by all_views instead of no posts.
My query goes as follows but it's not working as I want. In short what I want to achieve is to show posts by weekly_views custom field but if there's no post then show posts by all_views. And also if there's less than 12 posts by weekly_views then show weekly_views posts first and then remaining posts by all_views.
$args = array(
'post_type' => array( 'custom_post_type_1', 'custom_post_type_2'),
'posts_per_page' => '12',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'weekly_views',
),
array(
'key' => 'all_views',
),
),
);
The above code is returning me posts but are sorted by all_views.
Edit
The new query that's working for me
<?php
$args = array(
'post_type'=> array( 'custom_post_type1', 'custom_post_type2'),
'posts_per_page' => '12',
'meta_key' => 'weekly_views',
'orderby' => 'meta_value_num',
'order' => 'DESC',
);
$the_query = new WP_Query( $args );
if ($the_query->post_count < 12) {
$countweeklyposts = $the_query->post_count;
$showallpostscount = 12 - $countweeklyposts;
$args2 = array(
'post_type'=> array( 'band', 'artist'),
'posts_per_page' => $showallpostscount,
'meta_key' => 'all_views',
'orderby' => 'meta_value_num',
'order' => 'DESC',
);
$the_query2 = new WP_Query( $args2 );
}
?>
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
//Code to show posts goes here
<?php
endwhile;
wp_reset_postdata();
?>
<?php while ($the_query2 -> have_posts()) : $the_query2 -> the_post(); ?>
//Code to show posts goes here
<?php
endwhile;
wp_reset_postdata();
?>
You could do this too if you want a little less code
<?php
$args = array(
'post_type'=> array( 'custom_post_type1', 'custom_post_type2'),
'posts_per_page' => '12',
'meta_key' => 'weekly_views',
'orderby' => 'meta_value_num',
'order' => 'DESC',
);
$args2 = array(
'post_type'=> array( 'band', 'artist'),
'posts_per_page' => '12',
'meta_key' => 'all_views',
'orderby' => 'meta_value_num',
'order' => 'DESC',
);
if ($query->post_count > 12) {
$query_args = $args;
}else if($query->post_count < 12){
$query_args = $args2;
}
$query = new WP_Query( $query_args );
while ($query -> have_posts()) : $query -> the_post();
//Code to show posts goes here
endwhile;
wp_reset_postdata();
?>

Pagnation wordpress custom post type category page

Can't get the pagnation to work on the custom post types category page. It works when displaying the custom archive page. When I click on the pagnation it shows the posts from the first page but the URL says page=2.
This is the code i'm using in the archive-slug.php. How can I customize it to work with the taxonomy-slug.php?
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
query_posts( array( 'post_type' => 'dropshippers', 'paged' => $paged ) );
$loop = new WP_Query( array( 'post_type' => 'dropshippers', 'paged' => get_query_var( 'paged' ), 'posts_per_page' => 8 ) );
if(have_posts()) : while(have_posts()) : the_post();
//Posts
endwhile; endif;
if(function_exists('wp_pagenavi')) {
wp_pagenavi( array( 'query' => $loop ) );
} else {
echo "No posts";
}
You need to add the name of you category in your query, i prefer to use get_posts for that:
<?php $args = array(
'posts_per_page' => 8,
'offset' => 0,
'category' => '',
'category_name' => '',
'orderby' => 'date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'dropshippers',
'post_mime_type' => '',
'post_parent' => '',
'author' => '',
'post_status' => 'publish',
'suppress_filters' => true
);
$posts_array = get_posts( $args ); ?>
Fullfit this two line with your args:
'category' => '',
'category_name' => '',

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

Adding Pagination to Wordpress query for categories

I am trying to add pagination to a custom category list I have for a website gallery using WordPress. I have tried to follow a few guides but unable to add the paging function to my query. I have added my current queries.
Any help would be appreciated.
<?php
global $paged;
$curpage = $page ? $paged :1;
$args2 = array(
'taxonomy' => 'media-category',
//'parent' => '5',
'child_of' => 7,
'orderby' => 'date',
'order' => 'ASC',
'hide_empty' => 0,
'number' => 99,
'posts_per_page' => 3,
'paged' => $paged,
);
?>
<ul class="gallery-list small-block-grid-1 medium-block-grid-2 large-block-grid-3">
<?php
$categories = get_categories($args2);
foreach ($categories as $category) {
$args = array(
'post_type' => 'attachment',
'post_status' => 'any',
'posts_per_archive_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'media-category',
'field' => 'slug',
'terms' => array($category->name, 'featured'),
'operator' => 'AND',
),
),
);
$custom_query = new WP_Query($args);
?>
<?php while ($custom_query->have_posts()) : $custom_query->the_post(); ?>
I found the answer.
// Sets manual pagination
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$posts_per_page = 12;
$offset = ($posts_per_page * $paged) - 12 ;
$args = array(
'orderby' => 'id',
'order' => 'DESC',
'hide_empty' => 0,
'number' => $posts_per_page,
'offset' => $offset,
);
$categories = get_terms($taxonomies, $args);
This sets the page up to have 12 items per page and offsets the page number.

Resources