Wordpress: how to show two posts per custom post? - php

I have some custom post types. I want to loop through three of them and only show two posts per each post type!
The following code works, it gets the posts I want, however if there are 4 posts in 'humour' and 2+ in 'crime', the query never gets to 'romance' posts
$args = array( 'post_type' => array('humour', 'crime', 'romance'), 'showposts' => 6);
$query = new WP_Query( $args );
I did think about creating three different new queries (using WP_Query), but I have a feeling that would be an overkill! Is there another way to do this?

try this
"numberposts" => 2,

Related

Get posts with unique titles in WP Query

I have a query that takes posts of different types with the same title. Instead, I want to pull only posts with unique titles.
My query so far is as follows:
$args = array(
'post_type' => ['report', 'case-study', 'event'],
'posts_per_page' => $posts_to_get,
'post__not_in' => [$post->ID]
);
$query = new WP_Query($args);
However, this does not filter only unique titles.
I can get a list of unique titles using
$uniques = array_unique($titles); but that does not get me the actual posts. I wanted to know if there was a query method along the lines of 'post__title_not_in => $uniques;
I tried searching everywhere for this, but I was not able to find anything that worked. Would greatly appreciate any help.

Show all posts under a same custom field

So, I have a custom field called "geo_location".
And under that field, there are total 5 different values (1 to 5).
Now, there are total 50 posts that have one of the five values.
For example, post#1 has geo_location="1" and post#2 has geo_location="3" etc.
Is there a way to show all the posts under the same 'geo_location' value?
So for example, I want to show all the posts under the same geo_location="1".
how can I achieve this?
Thank you.
You need to use WP_Query to create a database query using your custom meta-data.
This is done like this:
$args = array(
'meta_key' => 'geo_location',
'meta_value' => '1'
);
$query = new WP_Query( $args );
Then you can output the result using the loop in the usual way.

Wordpress show multiple post types in loop

I'm creating a Wordpress site were I would like to show "tiles" with content from the site on the front page. These tiles are custom post types from the site like "our services", "consultants", "blog posts" and so on.
I know how to show one custom post type in Wordpress, but the problem is that I need to pull multiple post types in the same loop as I want them to be displayed in a matrix. Another problem is that I need to shuffle all the items in a random order, so that for example not all blogs just show in one place but all objects show after different items in random.
The third problem is that I need to show all items for a certain post type and just the latest for another. For example do I need to show all "our services" tiles, but only a couple of the "blog" tiles.
Is this possible to do, or can you not pull out records in this way using Wordpress?
Thank you for the help!
I suggest reading up on custom wordpress queries https://codex.wordpress.org/Class_Reference/WP_Query
For the first question you just need to specify
'post_type' => array( 'tiles', 'consultants', 'post' )
for the second question
'orderby' => 'rand'
so you will have something like
$args = array(
'post_type' => array( 'tiles', 'consultants', 'post' ),
'orderby' => 'rand'
);
$query = new WP_Query( $args );
For the third question - I'm not sure if it is possible to achieve with one query.
you can customise the things like this ,
$posttypes = array('post_typ1','post_typ2','post_typ3');
$randompost_typs = shuffle($posttypes);
$counter = count($posttypes);
for($i=0; $i<$counter;$i++) {
// suppose you want to show all posts from post_type1 then
if($randompost_typs[$i]=='post_typ1') {
$posts_per_page = -1;
} elseif($randompost_typs[$i]=='post_typ2') { // will work for 2nd post type
$post_per_page = 5; // show 5 posts from this post type
} else {
$post_per_page = 3; // show 3 posts from last post type
}
// here you will use the WP_Query class from wordpress
$args = array(
'post_type' => $posttypes[$i],
'orderby' => 'rand',
'posts_per_page' => $post_per_page
);
$query = new WP_Query( $args );
if($query->have_posts()) : while($query->have_posts()): $query->the_post();
// all the remaining wp loop content for example
the_title();
the_excerpt();
endwhile;
else:
echo 'no posts';
endif;
}
hope this will help, let me know if it has any issue.

Sort by category and then date on Wordpress homepage

My Wordpress site has multiple custom categories and posts types all which need to be displayed on the home page blogroll. But I need the first post on the homepage always to be the most recent post from a specific category.
For example the pst at the top of the page will always be the latest "giraffe" category; and below it; sorted default/chronologically are more "walrus" mixed with "seagull" and "nachos"
What would be the best way to accomplish this?
Without running through the Loop twice, sticky posts are your best option.
Otherwise:
...
$args = array(
'orderby' => 'post_date',
'category'=> 'giraffe',
'numberposts'=> 1,
);
$latest_giraffe_post = get_posts( $args );
...
then do what you're already doing for the rest

How can I loop through posts as well as child pages to display them all by date in Wordpress 2.9

Some background info --
In wordpress I have my portfolio as a parent page with each item of work a child page.
I also have a blog.
I want to display the most recent 6 items on the homepage whether they are from my portfolio or my blog.
I have a loop that displays the posts and on another page I have a loop that displays the child pages of a specific parent page. I only need to display the_title and the_post_thumbnail.
Is it possible to loop through both the posts and the child pages and display them in order of date (recent first).
So the final display would be a mixture of posts and child pages.
Could it be done by having a separate loop for pages and one for posts, then somehow adding the results to an array and then pull them out in date order (recent first).
Any help would be greatful.
Thanks
Use a custom query.
Example
$latest_content = new WP_Query(
array(
'posts_per_page' => 6,
'post_type' => 'any',
'post_status' => 'publish'
)
);
if ( $latest_content->have_posts() )
{
while ( $latest_content->have_posts() )
{
$post = $latest_content->next_post();
// Do anything you know about the loop.
print $post->guid . '<br>';
}
}
rewind_posts();
I have not tried it myself, but I think you can use the get_posts template tag with post_type => 'any'.
Like so (default ordering is date descending):
<?php
$args = array('post_type' => 'any', 'numberposts' => 6);
$posts_and_pages = get_posts($args);
?>

Categories