I have two post types, the regular posts and a custom post type. Everything is working fine and I show only 5 posts. One as a full post and four as excerpts. The problem I have is that the excerpts is showing the latest posts, independent of post category. I want to show two of the posts and two of the custom post type.
$args = array(
'post_type' => array( 'post', 'tutorial' ),
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) :
$count = 0;
while ( $query->have_posts() ) : $query->the_post();
if ( $count == 0 ) { ?>
<h2><?php the_title(); ?></h2>
<?php the_content();
$count ++;
} else { ?>
<h2><?php the_title(); ?></h2>
<?php the_excerpt();
}
endwhile;
endif;
wp_reset_postdata();
?>
Expected output should be the latest post as a full post, as it is working now. Then it should display the two latest posts of post type post and two latest posts of post type tutorial.
Basicly you only need to sort by posttype
$args = array(
'post_type' => array( 'post', 'tutorial' ),
'orderby' => 'post_type',
'order' => 'ASC',
);
If you want to keep the sorting of date as a secondary sort this should work (not tested).
$args = array(
'post_type' => array( 'post', 'tutorial' ),
'orderby' => array ('post_type' => 'ASC', 'order' => 'DESC' ),
);
For more information check the WP_Query documentation
Keep in mind that if you have 5 posts newer then any of your tutorials, none will show.
To guarantee 3 posts and 2 tutorials you will need to split the code in 2 wp_query loops with the posts_per_page parameter.
Related
I want to show a list of posts with the same terms as the post you are currently viewing (it's basically a related posts list).
It's a custom post type (called "oferta") and a custom taxonomy ("categorÃa").
I want to show 5 posts maximum (not all) as an ul list.
Using most of the code from https://wordpress.org/support/topic/query-cpt-that-share-the-same-taxonomy-as-the-current-cpt/ I've managed to get the list, but it has 3 problems:
It's not limiting to 5 posts
It's duplicating the li elements (1 correct element, 1 empty element)
It's showing the current post also on the list (I only want to show the other posts with the same term, not the current post)
For example, I get this on one of the posts:
I'm using the following code:
$custom_terms = wp_get_post_terms( get_the_ID(), 'categoria');
$args = array(
'post_type' => 'oferta',
'tax_query' => array(
array(
'taxonomy' => 'categoria',
'posts_per_page' => 5,
'field' => 'slug',
'terms' => $custom_terms[0]->slug,
),
)
);
$loop = new WP_Query( $args );
echo '<ul>';
while ( $loop->have_posts() ) : $loop->the_post();
echo '<li>'.get_the_title().'<li>';
endwhile;
echo '</ul>';
wp_reset_postdata();
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.
Can you please guide me how to show certain category posts on the footer. I am using custom post type news-site, taxonomy is news-category.
The link structure is abc.com/news-category/politics-news. The Politics news is a category page and all politics related news showing on the category page.
I don't know how to show 5 recent posts with same category on the footer.
I have tried with tag_id but nothing is showing.
Also i have tried this related post Related post but didn't work
Can you please guide me
Thanks
You can get the 5 posts of custom taxonomy by using the following logic.
<?php
$categories = get_the_category();
if ( ! empty( $categories ) ) {
$term_id = esc_html( $categories[0]->term_id );
}
$args = array(
'post_type' => 'news-site',
'post_status' => 'publish',
'posts_per_page' => 5,
'tax_query' => array(
array(
'taxonomy' => 'news-category',
'field' => 'id',
'terms' => $term_id
)
)
);
$the_query = new WP_Query( $args );
while ( $the_query->have_posts() ) : $the_query->the_post();
echo get_the_title();
endwhile;
?>
Don't forget to pass taxonomy_name in the query
So I followed this tutorial on how to use Wordpress, Jetpack, and Isotope to make a filterable portfolio of my projects I've done. I got the filter and project images to display just like the tutorial says it should. I think it works great.
My main issue right now is I only want to display my "Recent Websites" in my footer and only display the name, image, and brief content. I have done this a different way and it worked by creating a custom post type.
<?php $args = array( 'post_type' => 'projects', 'category_name' => 'websites', 'posts_per_page' => 2 );
Projects was a custom post type I created but since I followed that tutorial I got something all in one basically. Now again im trying to get only "2" of those recent websites to display at the footer of my website and this is how I did that.
<?php $args = array( 'post_type' => 'jetpack-portfolio', 'category_name' => 'websites', 'posts_per_page' => 2 );
When I view the website, nothing displays. I did more research and found out that "Jetpack-Portfolio" is a custom taxonomy and it's actually jetpack-portfolio-type but i'm trying to call it and nothing will work, OR it will display the two most recent projects I added to my site.
EDIT: I tried placing the pictures on here but my REP isn't high enough :P
SO my question is: how can I call that custom taxonomy to again display the websites only.
EDIT 7/16 # 9:06pm ET
<div class="Cfoot">
<h2>Recent Websites</h2>
<?php $args = array( 'post_type' => 'jetpack-portfolio', 'category_name' => 'websites', 'posts_per_page' => 2 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); { ?>
<section id="Recent-Web">
<img src="<?php echo get_template_directory_uri(); ?>/img/Foot_Placeholder.png" width="75" height="75" align="left" />
<h3><?php the_title(); ?></h3>
<p><?php the_excerpt(); ?></p>
</section>
<?php } endwhile; wp_reset_query();?>
</div>
EDIT 7/18
Ok so I was messing around with this some more and decided to change category_name to category_slug and now it seems to display 2 of my recent projects. That's fine but again I'm trying to only display from the websites category and not all of them. Here is my updated code.
<?php $args = array( 'post_type' => 'jetpack-portfolio', 'category_slug' => 'websites', 'posts_per_page' => 2);
What can I do to only display JUST the slug websites and not most recent projects.
If you combine the data sets that Jetpack Portfolios create in your database with the official WP_Query reference, the solution should be:
<?php
$args = array(
'post_type' => 'jetpack-portfolio',
'tax_query' => array(
array(
'taxonomy' => 'jetpack-portfolio-type',
'field' => 'slug',
'terms' => 'websites'
)
),
'posts_per_page' => 2
);
$loop = new WP_Query($args);
?>
I'm having a little trouble with a custom taxonomy template. I inherited a site that was developed by someone else and they use "Types" plugin to add some custom taxonomies.
Goal:
to have an archive template that shows only posts with a certain taxonomy term in it at example-domain.com/people/harrison-ford
Problem:
This code is bringing in posts that do not have the taxonomy selected.
Here's my full code:
<?php
$year = get_post_meta($post->ID, 'year', true);
$post_type = 'post';
$tax = 'people';
$tax_terms = get_terms( $tax );
if ($tax_terms) {
$args = array(
'post_type' => $post_type,
'people' => 'harrison-ford',
"$tax" => $tax_term->slug,
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1,
'orderby' => 'date',
'order' => DESC
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) : ?>
<h2 class="wwNews"><?php echo $tax_term->name; ?> News</h2>
<?php while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
<-- display stuff -->
<?php endwhile; // end of loop ?>
<?php endif; // if have_posts()
wp_reset_query();
}
?>
What are you expecting here? "$tax" is going to to be 'people' =>, which is going to overwrite 'harrison-ford' to the value of $tax_term->slug.
'people' => 'harrison-ford',
"$tax" => $tax_term->slug,
Furthermore, I don't know of any custom argument called people, I'm pretty sure that you want tax_query:
'tax_query' => array(
'taxonomy' => 'people',
'terms' => array('harrison-ford', $tax_term->slug)
)
Which will give you the results of all people matching harrison-ford and the value of $tax_term->slug within the taxonomy of people