I'm ordering my posts by a custom meta value named "size".
$querystr = "
SELECT $wpdb->posts.*
FROM $wpdb->posts, $wpdb->postmeta
WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id
AND $wpdb->postmeta.meta_key = 'size'
AND $wpdb->posts.post_status = 'publish'
AND $wpdb->posts.post_type = 'post'
AND $wpdb->posts.post_date < NOW()
ORDER BY $wpdb->postmeta.meta_value DESC
";
$pageposts = $wpdb->get_results($querystr, OBJECT);
if ($pageposts):
global $post;
foreach ($pageposts as $post):
setup_postdata($post);
the_title();
endforeach;
endif;
wp_pagenavi(); //creates page navigation
At the same time I'm using the WP-pagenavi plugin to navigate the posts by pages. I have 10 posts on each page.
The problem: Posts are ordered separately in each page. How can I order posts in descending order through all pages?
Update: I might have found a solution but I'm not sure how to implement it in my code
http://scribu.net/wordpress/wp-pagenavi/wpn-2-74.html
$my_query = new WP_Query( array( 'tag' => 'foo', 'paged' => get_query_var('paged') ) );
while ( $my_query->have_posts() ) : $my_query->the_post();
the_title();
// more stuff here
endwhile;
wp_pagenavi( array( 'query' => $my_query ) );
wp_reset_postdata(); // avoid errors further down the page
I've found a SO post that can solve your problem :
How to sort a 'query_posts' function by custom field, while limiting posts by another custom field
There you will find a custom class extending WP_Query and allowing you to make a query ordered by a custom field, and to include the paged query var too.
So the steps :
Past the class PostsOrderedByMetaQuery code somewhere like in your functions.php
Replace your query by :
// Retrieve `paged` in URL
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
// Make the query like in WP_Query but with our custom class
$query = new PostsOrderedByMetaQuery(array(
'post_type' => 'post',
'post_status' => 'publish',
'paged' => $paged,
'orderby_meta_key' => 'size',
'orderby_order' => 'DESC'
));
Use it !
while ( $query->have_posts() ) : $query->the_post();
the_title();
// more stuff here
endwhile;
wp_pagenavi( array( 'query' => $query ) );
wp_reset_postdata(); // avoid errors further down the page
Related
Newbie here...
I have a custom post type of 'equipe' (team in portuguese). I am trying to sort these alphabetically by post title then display the_title so we have a alphabetical list of names.
I've done a search on here and tried a few fixes but Im struggling to get anything other that the standard order.
Any help would be much appreciated!
<?php
$args = array('orderby'=> 'title', 'order' => 'ASC', 'post_type' => 'equipe', 'posts_per_page' => -1, 'post_status' => 'publish' );
$q = new WP_Query($args);
while ( $q->have_posts() ) : $q->the_post();
?>
<h3><?php the_title(); ?></h3>
<?php
endwhile;
wp_reset_query();
?>
<?php
$args = array( 'post_type' => 'equipe', 'posts_per_page'=>5, 'orderby'=>'post_title','order'=>'ASC');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
RESOLVED:
Ok the reason it was enforcing menu_order was because of a setting (F*ing checkbox) within the plugin Post Types Order.
I needed to un-check AUTO SORT
and check Use query ASC / DESC parameter
This then allowed me to adjust the array as follow (and discussed above):
$args = array('orderby' => 'title', 'order'=>'ASC', 'post_type' => 'equipe')
However I did need to add 'order'=>'ASC' into the other pages that sorted by the original query of menu_order.
I am trying to show all posts that contain a number.
I am using this code :
global $wpdb;
$postids = $wpdb->get_col($wpdb->prepare("
SELECT ID
FROM $wpdb->posts
WHERE SUBSTR($wpdb->posts.post_title,1,1) = %s
ORDER BY $wpdb->posts.post_title",REGEXP ^[0-9]));
if ($postids) {
$args=array(
'post__in' => $postids,
'post_type' => 'shows',
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
echo 'List of Posts Titles beginning with the letter '. $_GET['letter'];
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<p><?php the_title(); ?></p>
<?php
endwhile;
}
wp_reset_query();
}
I have tried this but it is not working it just 1 post, but when i change it to show for example posts that start with a it show all of them.
RLIKE ^[0-9] [0-9]%
Thanks.
To fetch only records starting with number, filter them using REGEXP
SELECT ID
FROM $wpdb->posts
WHERE $wpdb->posts.post_title REGEXP '^[0-9]+'
ORDER BY $wpdb->posts.post_title;
I am trying to get the latest posts on my blog. These are being displayed in a little widget in my front page on my website. I am getting a little trouble though. The posts are getting multiplied. http://goo.gl/Q2STSC, Here is my SQL query.
$posts = DB::connection('blog')->select("
SELECT a.post_title AS title, a.post_name AS slug, meta_value AS thumbnail,a.post_content AS contenido, a.post_date AS fecha
FROM wp_postmeta, wp_posts AS a
JOIN wp_posts AS b ON a.ID = b.post_parent
WHERE b.ID = wp_postmeta.post_id
AND meta_key = '_wp_attached_file' AND a.post_status = 'publish'
ORDER BY fecha
LIMIT 0 , 6");
Try this
<?php $args = array(
'posts_per_page' => 6,
'offset' => 0,
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish'
);
$posts_array = get_posts( $args );
foreach ( $posts_arrayas $post ) : setup_postdata( $post ); ?>
<li>
<?php the_title(); ?>
</li>
<?php endforeach;?>
Please use above code to fetch latest post from you blog.In above code you can see different parameters
posts_per_page : Number of post to get
offset : Starting Post number likeyou do in custom query limit 0[offset],6[posts_per_page]
order : Orderby [ DESC / ASC]
orderby : date [ column on which order clause has to perform]
post_type : post ( type of post] ( if you want to get posts for custom posts like news, then you can pass news as post_type
post_status : publish ( status of post )
Also in foreach you can permalink [ link of the post ] and the_title( title of the post)
For more details you can check here https://codex.wordpress.org/Function_Reference/get_posts
I need some help here as I've exhausted every place I can trying to find information. This is what I'm trying to do:
I have created a custom Post type in my admin called "Classes"
That works fine, the data works great and it's inputting in the admin.
I want to make a custom template to show this custom post type. However, everything I try it's not displaying properly. I've tried many code variations.
I know someones already done this and has the block of code to display this. This is what I need the code to do:
List All categories in my custom post type 'classes'
List all posts (show all content, not a link or excerpt) inside of each category.
Display it as such (I'm using Jquery Accordion)
the_category()
the_title()
the_content()
========================================================
By the way, Here is the block of code I'm currently using. It does work, but it ONLY shows the posts, all of them. It does not show the category with posts inside of them.
<?php
$type = 'classes';
$args = array (
'post_type' => $type,
'post_status' => 'publish',
'paged' => $paged,
'posts_per_page' => 10,
'ignore_sticky_posts'=> 1
);
$temp = $wp_query; // assign ordinal query to temp variable for later use
$wp_query = null;
$wp_query = new WP_Query($args);
if ( $wp_query->have_posts() ) :
while ( $wp_query->have_posts() ) : $wp_query->the_post();
echo '<h3 class="acc1">';
the_title();
echo '</h3>';
echo '<div class="sc"><div class="vs">View Schedule</div>';
the_content();
echo '</div>';
endwhile;
else :
echo '<h2>Not Found</h2>';
get_search_form();
endif;
$wp_query = $temp;
?>
Community, I need you. Please give your feedback!
What you want to do is actually start with a category query. You have to make sure you query all your categories with your custom post type:
Then for each category you would do pretty much what you have above.
$taxonomy = 'classes';
$args = array('hide_empty' => false,);
$terms = get_terms( $taxonomy, $args );
foreach($terms as $val) {
$term_id = $val->term_id;
$term_name = $val->name;
// now do post query
}
You most likely would have to display the category name as a header for your accordion as well.
Here's all the args for get_terms:
http://codex.wordpress.org/Function_Reference/get_terms
For that query you also most likely have to use a Simple Taxonomy Query (search for that on the page).
http://codex.wordpress.org/Class_Reference/WP_Query
By adding this arg to your above query:
'tax_query' =>
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => array( $term_name )
)
Is that what you were looking for?
There might be a better way to do this but I just had to recently do this and did pretty much what I just outlined here.
I should have been more clear and said to put the posts query within the foreach of the terms query.
Here's the updated answer based on your last reply (I have not tested this).
<?php
$taxonomy = 'classes';
$args = array('hide_empty' => false,);
$terms = get_terms( $taxonomy, $args );
foreach($terms as $val) {
$term_id = $val->term_id;
$term_name = $val->name;
$type = 'classes';
$args = array (
'post_type' => $type,
'post_status' => 'publish',
'paged' => $paged,
'posts_per_page' => 10,
'ignore_sticky_posts'=> 1,
'tax_query' =>
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => array( $term_name )
)
);
$temp = $wp_query; // assign ordinal query to temp variable for later use
$wp_query = null;
$wp_query = new WP_Query($args);
if ( $wp_query->have_posts() ) :
while ( $wp_query->have_posts() ) : $wp_query->the_post();
echo '<h3 class="acc1">';
the_title();
echo '</h3>';
echo '<div class="sc"><div class="vs">View Schedule</div>';
the_content();
echo '</div>';
endwhile;
else :
echo '<h2>Not Found</h2>';
get_search_form();
endif;
$wp_query = $temp;
}
?>
I am having issues with custom post type categories display.
I have created custom post type for review site
I want to show different categories in different tabs
but when I put any category of review in menu it shows all reviews
instead of showing reviews from specific category
For example:
I have created 2 categories in reviews
a) Games
b) software
Whenever I choose Games category it shows posts from software category too.
I had same issue with blog posts categories but I resolved that issue using code
in my category.php file
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$cat_id = get_cat_ID( single_cat_title(null, false) );
query_posts(array(
'post_type' => 'post',
'paged' => $paged,
'cat'=>$cat_id,
));
I have created taxonomy.php file for custom post type
<?php $mypost = array( 'post_type' => 'cpreviews','paged' => $paged);
$loop = new WP_Query( $mypost ); ?>
Can anyone please help us to understand what we need to do to display posts
according to categories for custom post types?
UPDATED CODE IN TAXONOMY.PHP but still have some issue:
I have changed above code under taxonomy.php to
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
//$currentTerm = $_GET[ 'term' ];
$cat_id = get_cat_ID( single_cat_title(null, false) );
$mypost = array('post_type' => 'cptreviews',
'paged' => $paged,
'tax_query' => array(
array(
'taxonomy' => 'product_reviews_product_category',
'terms' => (''),
'field' => 'slug',
'name' =>'Product Category',
)
)
);
$loop = new WP_Query( $mypost ); ?>
Now whenever I put category in 'terms' => ('kids') like this it shows all posts under that category only. but I want to take that 'terms value' dynamically.
Try this one:
<?php
$type = 'cpreviews';
$args=array(
'post_type' => $type,
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<p><?php the_title(); ?></p>
<?php
endwhile;
}
wp_reset_query(); // Restore global post data stomped by the_post().
?>
Supposing you have Custom Post Type: cpReviews --- Custom Taxonomy: RevCategories --- Create new review post and chose category from RevCategories. Query cpReviews will definitely show all the posts, you need to do some thing like this -----
query_posts(array(
'post_type' =>'cpreviews', //Custom_Post_TYpe
'showposts' => $limit,
'RevCategories' => 'Games',)); //Custom Post Type TAxonomy (can use page name here get_query_var('pagename'); for dynamic content
while (have_posts()): the_post(); global $post; echo the_title(); endwhile;
I have resolved this issue by creating taxonomy-{taxonomy}.php file & removed tax query code..it automatically takes given category..thanks all for your help
This will resolve this issue.
$args = array(
'post_type'=> 'post',
'cat' => 'Games'
);
$the_query = new WP_Query( $args );
if($the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();