I have two CPTs, one called 'artist' and the other called 'release.' I've created a single-artist.php page that displays an artist and its' custom meta data. On that same page I am displaying all releases by that artist with the following code:
<!-- GET RELEASES -->
<?php
$category = get_the_category();
$artist_name_slug = $category[0]->slug;
$args = array ('post_type' => 'release', 'posts_per_page' => 20, 'category_name' => $artist_name_slug);
query_posts ($args);
?>
<?php if (have_posts()) : ?>
<h3 class="artist-col2-title">Releases</h3>
<?php while (have_posts()) : the_post(); ?>
<div class="artist-release"><?php echo the_post_thumbnail('small'); ?></div>
<?php endwhile; ?>
<?php endif; ?>
<div style="clear:both;"></div>
Within the release CPT I have a release date in the meta data.
I would like to sort the releases based on that date but I cannot figure out how to add that to my arguments. Any help would be greatly appreciated!
To sort by meta data you can use
$args = array (
'post_type' => 'release',
'posts_per_page' => 20,
'category_name' => $artist_name_slug,
'meta_key' => 'your_meta_key' // i.e. release_date
'orderby'='meta_value' // for numeric value use 'meta_value_num' instead
);
query_posts ($args);
But notice the meta_key should be present in the query that you want to use for sorting, see for more.
Related
In the post loop I have a foreach loop which displays selected team members in the admin(from the "team" custom post type) from an ACF post object.
What I want to achieve is to display the post-publish date inside the foreach.
The problem is that the code echo get_the_date('M d, Y'); shows the date of the "team" custom post type and not the blog post date because is in the foreach.
I tried to pass the post id but it's shows the posts id of the "team" post types and not the actual "post".
How can I show the 'post_type' => 'post' publish post date inside in the foreach?
<?php
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 10,
'orderby' => 'date',
'order' => 'DSC'
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$postAuthors = get_field('select_post_author'); // ACF post object - custom post type - team
if( $postAuthors ): ?>
<?php foreach( array_slice($postAuthors, 0, 1) as $post): ?>
<div class="post-date">
<span>
<?php
echo get_the_date('M d, Y'); // here I want to get the post publish date but it show the publish date of the team post type items
?>
</span>
</div><!-- /.post-date -->
<?php endforeach; ?>
<?php endif; ?>
<?php endwhile;
wp_reset_postdata();
?>
Thanks
So the issue is due to $post being a global variable. I assume ACF stores an array of posts for get_field('select_post_author') then you need to change your foreach statement local variable name, e.g:
<?php foreach( array_slice($postAuthors, 0, 1) as $postAuthor): ?>
<div class="post-date">
<span>
<?php echo get_the_date('M d, Y', $postAuthor->ID); ?>
</span>
</div><!-- /.post-date -->
<?php endforeach; ?>
I solved the issue by passing the post id in the date.
<?php
$pid = get_the_ID(); //get the post id
$postAuthors = get_field('select_post_author');
if( $postAuthors ): ?>
Pass the post id in the get_the_date.
echo get_the_date('M d, Y',$pid);
I'm at a loss. On my single{custom-post-type}.php I want to create a loop that first shows the title of the page and the content. But then it also generates a list with all the content from a tag that had the same name as the title of the page.
Can somebody help me on the way?
You need an instance of WP_Query to query the posts inside the taxonomy term with the same name as the title. This is done by adding the tax_query field to the arguements.
<?php
// WordPress header
get_header();
the_post();
$args = array(
'post_type' => 'custom-post-type',
'posts_per_page' => -1, // -1 retrieves all the posts the query finds.
'tax_query' => array(
array(
'taxonomy' => 'category', // This is the default 'post' taxonomy, if you are using a custom taxonomy then you need to change this.
'field' => 'name', // Use name as you want to match the title
'terms' => get_the_title(), // Get the title of the current post as a string.
)
),
);
$query = new WP_Query($args);
if($query->have_posts()):
while($query->have_posts()): $query->the_post(); // Loop through the posts from the term with same name as current post title. ?>
<article class="post">
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
</article>
<?php endwhile;
wp_reset_postdata(); // Reset usage of 'the_post()'
endif;
<?php get_footer();
I'm using this wp_query. I want to show 2 posts on my sidebar, but, it is showing only one - the loop have any wrong config? thank you!
<?php
$destaque = new WP_Query('post_type=post&posts_per_page=1&cat=2,3,4,5');
if($destaque->have_posts()):
while($destaque->have_posts()):
$destaque->the_post();
?>
<div class="col-md-12">
<?php get_template_part('content','homepost'); ?>
</div>
<?php
endwhile; wp_reset_postdata();
endif;
?>
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => 2,
'category__not_in' => array(1),
'category__in' => array(2,3,4,5),
'offset' => 1
);
$secundarias = new WP_Query($args);
if($secundarias->have_posts()):
while($secundarias->have_posts()):
$secundarias->the_post();
?>
<?php
endwhile;
wp_reset_postdata();
endif;
?>
Don t know why you have two queries, but it would simply be
$new_query = new WP_Query();
$new_query->query('post_type=post&showposts=2');
or in your example
('posts_per_page=2');
On your first query you set that to "1".
Use a single query with that before your loop and it should work.
Also check your categories - sometimes it is just
I'm using wordpress and have a custom archive page for a custom post type.
The custom loop gets the logged in users registration date and only shows posts that were published on or after they registered. Works great.
However, the pagination is still using the main query so if there are 4 posts in total set to 2 posts per page the pagination always shows there are two pages even if only one post is displayed due to the logged in users registered date.
Can anyone help me modify what I have so the pagination only shows for results in more than 2 posts for that users query? I've been trying for hours now using various changes I've found on the web...
<?php if ( have_posts() ): ?>
<?php
# Get the current user's info
$user_info = get_userdata(get_current_user_id());
# Use date_parse to cast your date to an array
$regdate = date_parse($user_info->user_registered);
# Set your arguments for WP Query
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'inner',
'posts_per_page' => '2',
'posts_per_archive_page' => '2',
'paged' => $paged,
'date_query' => array(
array(
'after' => array(
# Setting date to array above allows to call specific values within that date
'year' => $regdate['year'],
'month' => $regdate['month'],
'day' => $regdate['day'],
),
# Include posts from the day the user registered
'inclusive' => true,
),
),
# Display all posts on a single page.
);
$my_query = new WP_Query( $args );
while ($my_query->have_posts()) : $my_query->the_post();
get_template_part( 'template-parts/content', get_post_format() );
endwhile; ?>
<div class="nav-previous alignleft"><?php next_posts_link( 'Older posts' ); ?></div>
<div class="nav-next alignright"><?php previous_posts_link( 'Newer posts' ); ?></div>
<?php else: ?>
Nada
<?php endif; ?>
Working with Custom Archive and Pagination
#Scott Eldo Your approach with create custom query will not change main query on your custom archive. FYI and you are correct, pagination only work for main query.
In your case, the recommended approach, I will use filter pre_get_posts to work with custom archive and pagination. Please take a look my answer here how to modify main query on post type archive page, you can figure it out with your query parameters.
BUT if you intent to create query direct into your template ( even it is not change main query ), you need to match your custom query with $GLOBALS['wp_query'] that use in pagination and don't forget to use wp_reset_query() ( MUST ). Take a look my approach here related with your code:
<?php if ( have_posts() ): ?>
<?php
# Get the current user's info
$user_info = get_userdata(get_current_user_id());
# Use date_parse to cast your date to an array
$regdate = date_parse($user_info->user_registered);
# Set your arguments for WP Query
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'inner',
'posts_per_page' => '2',
'posts_per_archive_page' => '2',
'paged' => $paged,
'date_query' => array(
array(
'after' => array(
# Setting date to array above allows to call specific values within that date
'year' => $regdate['year'],
'month' => $regdate['month'],
'day' => $regdate['day'],
),
# Include posts from the day the user registered
'inclusive' => true,
),
),
# Display all posts on a single page.
);
$my_query = new WP_Query( $args );
while ($my_query->have_posts()) : $my_query->the_post();
get_template_part( 'template-parts/content', get_post_format() );
endwhile; ?>
<?php
/**
* Fix Pagination in custom page/archive
* Set global wp_query the same as our custom query
*
* Use function the_posts_pagination( $args ); for pagination will work too
* see https://developer.wordpress.org/reference/functions/get_the_posts_pagination/#source-code
*/
$GLOBALS['wp_query'] = $my_query;
?>
<div class="nav-previous alignleft"><?php next_posts_link( 'Older posts' ); ?></div>
<div class="nav-next alignright"><?php previous_posts_link( 'Newer posts' ); ?></div>
<?php else: ?>
Nada
<?php endif; ?>
<?php wp_reset_query(); ?> // MUST use to reset global query and post data
Another approach and still NOT recommended is use query_post for your custom query. More reading about it you can learn more in here.
Currently using the Wordpress Plugin [Event Post Type] and the posts are currently ordered by when I post them. I really need them ordered by event date which the plugin adds to the post type for me.
I am currently using this code and am not sure how to get it to order:
<div id="events-teaser"><?php
$args = array( 'post_type' => 'event', 'posts_per_page' => 10, 'orderby' => '_date_start', 'order' => 'DESC' );
$loop = new WP_Query( $args );
if ( have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post();?>
<div class="teaser-event <?php echo do_shortcode('[xydac_field]promo[/xydac_field]'); ?>">
<div class="event-meta gold">
<div class="event-date"><?php echo get_post_meta($post->ID, "_date_start", true); ?></div>
<div class="event-time"><?php echo get_post_meta($post->ID, "_time_start", true); ?></div></div>
<div class="event-title"><?php the_title(); ?></div></div><?php
endwhile; else:
?><p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
</div>
Place a call to query_posts() in one of your Template files before The Loop begins. The wp_query object will generate a new SQL query using your parameters. When you do this, WordPress ignores the other parameters it receives via the URL (such as page number or category). If you want to preserve that information, you can use the $query_string global variable in the call to query_posts().
For example, to set the display order of the posts without affecting the rest of the query string, you could place the following before The Loop:
global $query_string;
query_posts( $query_string . '&order=ASC' );
change its order from 'ASC' to whatever you want.