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.
Related
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.
I've got a List of custom post types which I am displaying on my site. Additionally to those post types, I added a plugin to my WordPress which allows me to add a thumbs up rating system to each post type, so that it looks like this:
The code for looks like this:
<?php
/* The custom post types query */
$query = new WP_Query( array(
"post_type" => "motto",
"order" => "ASC",
));
while($query -> have_posts()) : $query -> the_post();
?>
/* Container with the ratings from the plugin + each post type */
<div id="motto-container">
<?=function_exists('thumbs_rating_getlink') ? thumbs_rating_getlink() : ''?>
<h3 class="abimottos">
<?php echo get_post_meta($post->ID, 'motto_titel', true); ?>
</h3>
</div>
I've got a list of these custom posts + their ratings, and of course each post has an individual rating and I want to order my custom post types after the value of those ratings. How could I archive that?
I know that the meta_key for the ratings is _thumbs_rating_up (since I've already modified the value with the ARI Adminer plugin), can I somehow use this meta_key to order the custom post types after the meta_value of the ratings?
I'm pretty new to PHP and databases.
You are already using WP_Query to get the posts, so you can specify the meta_key to sort by in the $args array, e.g.
$query = new WP_Query( array(
'post_type' => 'motto',
'meta_key' => 'thumbs_rating_up',
'orderby' => 'thumbs_rating_up',
'order' => 'DESC'
));
Note that you need to include the key name in both meta_key and orderby. I also assume you want to sort in descending order to show the highest ratings first.
Ref: Wordpress Codex for WP_Query
Also, a note on the meta_key:
meta_keys prefixed with an underscore are private and hidden from the custom fields, so normally you would use the version without an underscore. This might not be the case here because I assume the rating can't be changed in the admin, but just make sure that the meta_key you need to use is actually _thumbs_rating_up and not thumbs_rating_up.
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