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.
Related
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.
Is there any possible way to sort a new Wordpress post query by the title, but numerically instead of alphabetically?
I have some titles that have a lot of the same name alphabetically, then have a number afterwards, so of course for example Wordpress is putting title12 ahead of title1.
$args = array(
'orderby'=> 'title',
'order' => 'ASC',
);
$loop = new WP_Query( $args );
I know we have this functionality to sort titles in ascending order, but it does not sort titles like that:
Title 1
Title 2
Please let me know if we any work around using WP query ?
Thanks for your help in advance :)
Try adding this immediately after your code above:
usort($loop->posts, function($a,$b) {
return strnatcmp($a->title, $b->post_title);
});
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,
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
I have the following function that I've added to my functions.php file in WordPress. The idea is that it gathers all of the titles of 'fsmodel' posts (a custom post type that I've created). It then returns these as an array, which I then use to populate a select tag in the custom meta fields for a second custom post type.
Basically, 'fsmodel' will have posts with a boat model, and the 'fsboat' post type will have a drop-down with the names of each of the models to select from.
Now, this appears to works fine in the Dashboard - the drop-down is populated as expected. When I save, however, the post doesn't show up in the Edit list. Also on the website, all pages output as the 404 error page when this function is active.
I'm certain that the problem lies within the following code - does anyone have any idea what I might have done wrong?
function fs_model_array() {
$models_array = array();
$loop = new WP_Query(array(
'post_type' => 'fsmodel',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
'post_status' => 'publish'
));
while ( $loop->have_posts() ) : $loop->the_post();
$models_array[] = get_the_title();
endwhile;
return $models_array;
};
OK, I've come up with a solution (I hope - it's holding up for now).
Instead of creating a loop, I've just used the $wpdb->get_results to search the database for the column with a WHERE filter for the custom post type.
Then run an array builder:
$models_array = array();
$model_db = $wpdb->get_results("SELECT post_title FROM $wpdb->posts WHERE post_type='fsmodel' AND post_status = 'publish'");
foreach ($model_db as $model_db) {
$models_array[] = $model_db->post_title;
}
Thanks again for your time, hsatterwhite! :-)
I think you might find that adding wp_reset_query() to the end of your function will solve your problems :)
I like your solution, but I'd be inclined to say that you need to call the global variable of $post whenever you use the loop like this in a function, as it assigns it to that variable.
function fs_model_array(){
global $post;
$models_array = array();
$loop = new WP_Query(array(
...