WP Query post by year in accordion - php

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,

Related

WP_Query by an array or list of post slugs

Is this possible, I couldn't find the question asked anywhere on the web.
If you had a list of posts by their slug, example:
$postslugs = array("page-one","page-two","page-three");
And you wanted to create a single query to find all these specific pages:
$args=array(
'post__not_in' => array($post->ID),
'posts_per_page'=> -1,
'post_type' => 'customtype',
'post_status' => 'publish',
'pagename' => $postslugs
);
$my_query = new wp_query( $args );
The result should come back with the three pages. This of course would not work the way it's written now. Is this possible?
If you take a look at the documentation for the WP_Query() class, specifically the Post & Page Parameters section, you'll see it has a handy parameter called post_name__in which takes an array of post_name (aka slugs). Also, are you sure you need to use post__not_in (and maybe posts_per_page since you're defining an array of slugs to get already?
$names = array( 'page-one', 'page-two', 'page-three' );
$query_args = array(
'posts_per_page' => -1,
'post_type' => 'customtype',
'post_status' => 'publish',
'post_name__in' => $names
);
$my_query = new WP_Query( $query_args );
I would like to notice, that there is some issue with old WordPress versions where post_name it is not = to a post_slug.
For example if you try to create new post with title Customer.io it will create a post with name customer-io but slug will be customerio the same will be for this Pokéapi
The same will be if you change post slug after you have created the post.
So the best way is to check manually inside loop of your WP_Query.
$desired_array_of_slugs = array( 'page-one', 'page-two', 'page-three' );
$query_args = array(
'posts_per_page' => -1,
'post_type' => 'customtype',
'post_status' => 'publish'
);
$my_query = new WP_Query( $query_args );
Then
foreach($my_query as $post) {
if(in_array(sanitize_title($post->name)], $desired_array_of_slugs, true)){
//do ur staff
}
}

add_meta_box mixed up custom query post_id with global post_id

I have created two groups of metaboxes (metabox1 and metabox2) within a CPT. In one of them (metabox2) I run a custom query to retrieve a bunch of post ids to show them in a select fild.
The problem arise when the other metabox (metabox1) picks up the one of the post_id retrieved from the custom query instead of the post_id in the global environment, so eventually this metabox1 cannot retrieve the data from the db because the post_id is wrong.
This the custom query and I've put different reset postdate, but they don't seem to work:
$podcasts = [];
$args = array( 'posts_per_page' => 100,
'orderby' => 'date',
'order' => 'desc',
'post_type' => 'podcast',
'post_status' => 'publish',
'suppress_filters' => true );
$my_query = new WP_Query( $args );
if($my_query->have_posts()):
while($my_query->have_posts()):
$my_query->the_post();
$podcasts[] = [get_the_ID(),get_the_title()];
endwhile;
endif;
$my_query->reset_postdata();
wp_reset_postdata();
// wp_reset_query();
return $podcasts;
}```

Order Relation ACF by post_title

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

Limit posts per page after using array_merge in Wordpress

I have two post types I want to display, Posts and then a Custom Post Type called 'Notes'. I want to query both of these and display them together. I've currently got it working using array_merge.
I want to create a new query so I can choose how many posts to display per page and also get pagination working. I've tried various different things to limit the amount of posts displayed but can't seem to crack it.
Here is my code:
$q1_args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'post_status' => 'publish'
);
$q1_posts = get_posts( $q1_args );
// get the posts for the second query
$q2_args = array(
'post_type' => 'notes',
'posts_per_page' => -1,
'post_status' => 'publish'
);
$q2_posts= get_posts( $q2_args );
// Merge the post arrays together, and sort by date using the order_by_date function
$final_posts = array_merge( $q1_posts, $q2_posts );
usort( $final_posts, 'order_by_date' );
// Loop over the posts and use setup_postdata to format for template tag usage
foreach ( $final_posts as $key => $post ) {
$post_type = $post->post_type;
setup_postdata( $post );
//DO STUFF
}
Any thoughts on how I can limit posts per page and get pagination working?
Is there any particular reason this can't be done like this?
$args=array(
'post_type' => array('post', 'notes'),
'posts_per_page' => 15, //or any other number you want per page
'post_status' => 'publish',
'orderby' => 'date',
'order' => 'DESC',
'paged' => (( get_query_var('page') ) ? get_query_var('page') : 1)
);
$posts=get_posts($args);
if ($posts->have_posts())
{
while ($posts->have_posts())
{
$posts->the_post();
//DO STUFF
}
//add pagination here
}
else
{
// no posts found
}
wp_reset_postdata();

How to limit WP_Query results to last X number of posts published?

My aim is to limit the results of my query to the last X number of posts published.
So far, my query looks like this:
$args = array(
'post_type' => 'post'
);
$query = new WP_Query( $args );
Is there a parameter I can use or should I write a custom SQL query?
Ref: http://codex.wordpress.org/Class_Reference/WP_Query#Parameters
One of the parameters WP_Query takes is posts_per_page. It sorts by most recent post by default so you should only need the posts_per_page parameter.
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => '5'
);
$query = new WP_Query( $args );
Solution :
$args = array('post_type' => 'post','posts_per_page' => 'your no of post', 'orderby' => 'ID', 'order' => 'DESC');
$query = new WP_Query( $args );
AND FOR Custom Query
global $wpdb;
$qyery="your custom query here";
$wpdb->query($query); Use this function
or
$wpdb->get_results( $query, output_type );

Categories