Order By with have_post wordpress - php

I have ids of some posts id array that have higher priority for me.
Now on listing all posts i want that array ids should come first then all other ids post.
I have logic set for that order by like i have array with two ids (15,19) then i want those two post first then other all id posts should come.
My logic is below and it works well.
ORDER BY CASE
WHEN id IN(15,19) THEN 0
ELSE id
END
But how can i add this logic with have_posts loop order by.
Please help me.

Thinking fast about it I would make two loops, one with the posts you want fixed in the beginning and the second with the rest of the posts like this:
$fixed = array(8,14);
//gets only the posts with IDs 8 and 14
$fixed_args = array(
'post__in' => $fixed
);
$fixed_query = new WP_Query($fixed_args);
if($fixed_query->have_posts()){
while ($fixed_query->have_posts()){
$fixed_query->the_post();
the_title();
the_content();
}
}
//excludes the posts with IDs 8 and 14
$query_args = array(
'post__not_in' => $fixed
);
$the_query = new WP_Query($query_args);
if($the_query->have_posts()){
while ($the_query->have_posts()){
$the_query->the_post();
the_title();
the_content();
}
}
I don't know if there is a way to do this in just one simple loop, but I will look into it and update if I find anything.

Related

Search WordPress posts with wildcard in a given category for specific sentence and output unique results

For example I've got the Metals category in my WordPress installation with different metals names posted here and there within this category in different posts, like Metal (brass), Metal (aluminum) and so on.
What I need is to output all unique mentions of all metals from all posts from that category, skipping specific ones.
As far as I understand, I should create an array with all metal mentions, sort it like I want, keep only unique names and then output the array's content using
<?php foreach($my_array as $key => $value) {echo $value.', ';} ?>
There is no problem for me to prepare the final array using something like
<?php $my_array = array_unique(($metals_array), SORT_REGULAR); ?>
but I have no idea how to fill this array with required data, searching posts for Metal (*), and skipping, say, Metal (steel) in the output.
Please try this:
<?php
$title_array = array();
$args = array('post_type' => 'post', 'category_name' => 'metals', 'orderby'=> 'title', 'order'=>'ASC' );
$query = new WP_Query($args);
if($query->have_posts()):
while($query->have_posts()) : $query->the_post();
$title_array[] = get_the_title();
endwhile;
endif;
$unique_array = array_unique($title_array);
if($unique_array):
foreach($unique_array as $unique) {
echo $unique;
}
endif;
?>
It is the query for simple posts based on category but if you have created a 'Custom Post Type' then the query will be changed, otherwise it will work for you.
'category_name' => 'category-slug'. Category name consists the category slug.
Hope this may help you.

Alternate between 2 categories in wordpress

I am wondering if there is a way to list posts from 2 categories in wordpress such that every fifth post is from different category. This is what I mean
Category B
Category A
Category A
Category A
Category A
Category B
and so on....
The sorting for each category will be based on date (descending)
Well its much complicated then its sounds, you wont find an exact method to do this, but you can work around it. For instance in your query you can get posts from 2 categories and then in the loop you can count them and if count is less than 5 display category A, else display category B. But the problem with this is that you dont know how many posts is it going to fetch from each category. It might fetch 2 posts from Category A and 3 From category B. So Instead its better if you use 2 different loops and save all the data in an array.
<?php
$args1 = array( 'posts_per_page' => -1, 'category' => 1 );
$args2 = array( 'posts_per_page' => -1, 'category' => 2 );
$posts_of_category_A = get_posts( $args1 );
$posts_of_category_B = get_posts( $args2 );
$check = 0;
foreach($posts_of_category_B as $posts_B){
// This will only show 1 post of category B;
// echo $posts_B->title (var_dump it)
for($i = $check; $i<$check+4; $i++ ){
// This will Show only first 4 posts of Categroy A
// Convert object to array, something $posts_B[$check]->title
}
$check += 3;
}
Its just a concept, there might be several changes to be made to this script. I know its kind of a hacky way to do this, but i cant think of a better 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.

Wordpress: displaying 25 posts from one category and 55 from another

I'm trying to make a loop that will get the latest 25 posts from one category and the latest 55 from another, they need to be in the same loop and the posts are in a isotope filtering system and when it is on 'all' it needs to display all the categories in date order not category type>post date, is there a trick for doing this?
Thanks, Harry.
EDIT:
Here is what I currently have for my get_posts loop;
<?php
global $post;
$myposts = get_posts('cat=10,11,49&numberposts=50');
foreach($myposts as $post) :
setup_postdata($post);
$cat_name = '';
$category = get_the_category();
$cat_name = $category[0]->slug;
?>
<?php endforeach; ?>
I have tried a few logical things such as;
$myposts = get_posts('cat=10,49&numberposts=50', 'cat=11&numberposts=50');
But this just returned posts from category 10 and 49.
I thought maybe something like this could work but need the right syntax for it;
$myposts11 = get_posts('cat=11&numberposts=50');
$myposts1049 = get_posts('cat=10,49&numberposts=50');
$myposts = $myposts1049 + $myposts11;
The issue with what you've tried is you've added two post arrays together as if they were numbers;
$myposts = $myposts1049 + $myposts11;
You should be using array_merge to merge the two arrays into a single array and loop through that instead;
$myposts = array_merge( $myposts1049, $myposts11 );
EDIT
Looking at the Wordpress StackExchange site, Otto has answered a very similar question here;
https://wordpress.stackexchange.com/questions/130009/how-to-merge-two-queries-together#answer-130055
And additionally used array_unique to eliminate duplicates before using the new array in a WP_Query object.

Looping through Wordpress posts with in_category filter doesn't get me all of the posts

I'm trying to get some posts from a specific category into a multidimensional array like so:
wp_reset_query();
query_posts();
while (have_posts()) : the_post();
if (in_category('videos')) {
$postAttribute["permalink"] = get_permalink();
$postAttribute["image_url"] = wp_get_attachment_url(get_post_thumbnail_id($post->ID));
$postAttribute["title"] = get_the_title();
$postAttribute["comments_number"] = get_comments_number();
array_push($videos, $postAttribute);
};
endwhile;
and then to check the array I run:
echo count($videos);
I keep getting 2 as a result, even though I know there are way more posts than that in the interview category.
I checked the max number of posts setting, set it higher just to see, but still got nothing.
Any idea what I could be missing?
It looks like you're relying on the main query, which by default gets you only 20 posts per page or so.
Do a custom query instead:
$my_query = new WP_Query(array(
'posts_per_page' => -1, // all
'category_name' => 'videos',
));
while($my_query->have_posts()){
$my_query->the_post();
// do your thing here
}
in_category() check is not required, because the query will get you only posts from the "videos" category

Categories