My meta_query is ordering just by ID, but I need order by the post_title
Heres the query I'm using
<?php
$args = array(
'orderby' => 'meta_value', // Or post by custom field
'order' => 'DESC', // Or post by custom field
'meta_key' => 'case-solucao',
'post_type' => 'testemunho', // Just the post type
'posts_per_page' => -1, // Show all available post
);
query_posts($args);
while(have_posts()){
the_post();
$solucoesCases = get_field('case-solucao');
foreach ($solucoesCases as $solCase) {
// echo count($solCase->ID);
}
echo $solCase->ID ." - ". $solCase->post_title."\n";
}
?>
I have a Custon Field that's link the solution to post_type. But Its only sort by ID, and I need to sort by Post_title.
I'm really lost. Hope someone could help me.
Try this. If, I'm correct, you need to display the title along the your custom field.
$args = array(
'orderby' => 'title', // This will order the post by title
'post_type' => 'testemunho', // Just the post type
'posts_per_page' => -1, // Show all available post
);
query_posts($args);
while(have_posts()){
the_post();
foreach ($solucoesCases as $solCase) {
$solucoesCases = get_field('case-solucao', $solCase->ID ); // add the ID of the post_type
echo $solCase->ID ." - ". $solCase->post_title."\n"; // will print all the title
echo $solucoesCases;
}
}
Related
I have a custom post type of products, and each product belongs to a product category. I am trying to query the products to get all products sorted alphabetically by their product category i.e. First: Product: Lion
Category: Animals
Last:
Product: Snowmobile
Category: Winter
I am using a custom field for the product category, but my query doesn't sort them alphabetically - but instead by which date they are published. The product_cat_meta field is a regular text field set up in custom fields. Query is here:
function get_products()
{
$args = array(
'post_type' => 'products',
'post_status' => 'publish',
'meta_key' => 'product_cat_meta',
'orderby' => 'meta_value',
'order' => 'ASC',
'posts_per_page' => -1
);
$products = new WP_Query($args);
if ($products->have_posts()) {
$index = 0;
while ($products->have_posts()) {
$products->the_post();
$prod_meta = get_field('product_cat_meta');
echo $prod_meta;
);
$index++;
} // end while
} // end if
}
The result of this query just returns the prod category in the way they are set up in wordpress – the latest posts first, but not sorted alphabetically
You can try to put the code in functions.php file.
The code, which can be dropped into your current theme’s functions.php if you like:
function get_products()
{
$args = array(
'post_type' => 'products',
'post_status' => 'publish',
'meta_key' => 'product_cat_meta',
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page' => -1
);
$products = new WP_Query($args);
if ($products->have_posts()) {
$index = 0;
while ($products->have_posts()) {
$products->the_post();
$prod_meta = get_field('product_cat_meta');
echo $prod_meta;
);
$index++;
} // end while
} // end if
}
Get More details, follow the link: https://codex.wordpress.org/Alphabetizing_Posts
I'm trying to get the testimonials (custom post type) in following order.
I can easily retrieve posts using WP_Query class but struggling to create an accordion as shown in the screenshot above.
Try using a custom select query and looping through each post with a post_type of testimonial.
Then loop through result and set WP_Query() class date_query param to an array of the years obtained by the custom select query result.
global $wpdb;
$posts = $wpdb->posts;
//Get all unique years as "years" from posts where post type is equal to testimonials
$sql = "SELECT DISTINCT(YEAR(`post_date`)) as years FROM $posts WHERE post_type = 'testimonials' ORDER BY years DESC"; //Get all post year list by DESC
//Loop through all results and use date_query param https://codex.wordpress.org/Class_Reference/WP_Query#Date_Parameters
$result = $wpdb->get_results($sql);
foreach($result as $rs) {
echo '<h2>'.$rs->years.'</h2>';
$args = array(
'post_type' => 'testimonials',
'post_per_page'=> -1,
'post_status' => 'publish',
'orderby' => 'date',
'order' => 'DESC',
'date_query' => array(array(
'year'=> $rs->years,
),),
);
$loop = new WP_Query($args);
if($loop->have_posts()) {
while($loop->have_posts()) : $loop->the_post();
echo ''.get_the_date().'';
endwhile;
}
}
#user3325126 Great code, thanks :)
Just replace 'post_per_page'=> -1, into 'posts_per_page'=> -1, (missing one s)
It should be
'posts_per_page'=> -1,
I am working in a project where I am using below code to limit number of posts in each category. But I can't figure out how to sort these categories by latest posts. My code is working fine but when I add argument in wp query array, it does not sort or it breaks. I want to order the category with most recent post (date).
Can anyone guide me?
<?php $terms=g et_terms( array( 'taxonomy'=>'category', 'hide_empty' => false, ) ); foreach($terms as $cat){ $cata_name = $cat->name; $term_id = $cat->term_id; ?>
<?php //echo '<h3>'.$catname[0]->cat_name.'</h3>'; ?>
<?php $catqueryy=n ew WP_Query ( 'cat='.$term_id. '&posts_per_page=5');$count=$ catqueryy->found_posts; ?>
You can add ORDER BY argument in wordpress as:
<?php
$args = array(
'post_type' => 'post', //your_post_type
'orderby' => 'date', //meta_value
'meta_query' => array(array('key' => 'cat','value'=>$term_id)),
'order' => 'DESC',
'posts_per_page' => 5,
);
$catqueryy = new WP_Query($args);
?>
You can explore the wordpress document: https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters
I'm working on a page where I want to show posts (custom post type) from a category which have the same categoryname as the title of the post.
I'm still learning, that's Why I hope you guys could help me out. :)
What I have:
CPT named 'shoes'
CPT categories: sneakers, boots, sportshoes
CPT named 'articles'
CPT categories: sneakers, boots, sportshoes
On the page ~/sneakers/ (from CPT 'shoes') I want to show articles from the category 'sneakers'.
This what I have till now:
global $post;
$cat_ID = array();
$categories = get_the_category();
foreach($categories as $category) {
array_push($cat_ID,$category->cat_ID);
}
$args = array(
'orderby' => 'date',
'order' => 'DESC',
'post_type' => '[THE_TITLE]',
'numberposts' => -1,
'category__in' => $cat_ID
);
$cat_posts = get_posts($args);
if ($cat_posts) {
foreach ($cat_posts as $cat_post) {
?>
<li><?php echo $cat_post->post_title; ?></li>
<?php
}
}
OK, based on the question and the following explanation:
We are in the single-shoes.php where the information for single shoe is shown.
We want to grab and display the information about articles custom post type, and this articles must be in specific category, which name is = post_title.
If this is the case you can do the following:
We want our posts to be from articles custom post type so we must put 'post_type' => 'articles'in the $args array.
We want returned post to be in category, which name = post_title, so we must put 'category_name' => get_the_title() into $args
The code is:
<?php
$args = array(
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'articles',
'posts_per_page' => -1,
'category_name' => get_the_title()
);
$articles = new WP_Query($args);
if ( $articles->have_posts() ) :
while( $articles->have_posts() ) : $articles->the_post(); ?>
<li><?php the_title();?></li>
<?php
endwhile;
endif;
wp_reset_postdata();
?>
P.S. I'm still a little confused from the category name and still not sure that I understood correctly how this value is taken, however, if you can grab this value, the query is provided above.
I'm trying to do a query with the arguments below. Somehow, WP just doesn't return any posts. I can't figure out what I'm doing wrong! Any help?
Some more info: I have a custom post type that contain featured images. I want them to be displayed in a header slider. Through Advanced Custom Fields plugin I've created a custom field in the posts: 'assigned_page'. It's an array with page ID's on which that specific slide should be displayed. '$current_page' is the ID of the current page that's to be displayed. So, $args should filter the custom post type, and the posts that have the current page ID in their 'assigned_page' array.
// Get the current page ID
global $post;
$current_page = $post->ID;
$string_page = (string)$current_page;
$current_parent_page = $mv_is_subpage->ID;
// Post selection
$args = array (
'post_type' => $post_type,
'posts_per_page' => $posts_per_page,
'orderby' => $orderby,
'order' => $order,
'no_found_rows' => 1,
'meta_query' => array(
array(
'meta_key' => 'assigned_page',
'meta_value' => $string_page,
)
),
);
Then:
$query = new WP_Query( $args );
And then the loop:
while ($query->have_posts()) : $query->the_post();
I guess it should be like this (according to docs at http://codex.wordpress.org/Class_Reference/WP_Query) :
array(
'key' => 'assigned_page',
'value' => $string_page,
)