Alternate between 2 categories in wordpress - php

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.

Related

Getting a count of Top level Categories WordPress

i am trying to get a count of top level categories , i am using wordpress 2016 theme , its a static front page and i am trying to retrieve the total count of categories and then dynamically create table with those categories
i tried to search with WP_Query and wp_list_categories it does list names but not the total count , i tried to use count function in php but output is always 0(zero) even though i have few categories / posts etc already created to test this functionality.
please advise or assist with the code
Thanks
Try Below Code :
<?php
$args = array(
'parent' => 0,
'hide_empty' => 0
);
$categories = get_categories( $args );
echo count( $categories );
?>
You can also wrap it in a function to re-use it else where in your site.
Hope this will help you!

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.

Order By with have_post wordpress

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.

order results by category, wordpress

I have a category page called 'features' this is all fine, but the posts in the features category also belong to a certain genre of film, and it's this what I want to order the posts by on the actual features category template.
eg
features cat template brings in all features posts.
then what I want to do is display in alpha order by whatever genre it also belongs to
features
action
post
post
post
comedy
post
post
sci-fi
post
post
etc.
this is what I have at the moment ( the cat numbers relate to the genres = action=10 etc)
query_posts('cat=10,11,12,13,14,15,16,17,18&orderby=title&order=DESC' );
while (have_posts()) : the_post();
How can I list all the posts (group them) by genre ? when I use title here i guess it's using the posts title.
Playing around if I stick this in the post loop
foreach((get_the_category()) as $childcat) {
if (cat_is_ancestor_of(4, $childcat)) {
echo $childcat->cat_name;
}
}
this returns the actual genre cat for each post while in the loop, but I'm not sure how to stick it together so I can state the ordering of the posts by groups of genre, I was hoping I could do this in the query_posts?
Any help in the right direction would be greatly appreciated.
I would consider first doing a query for your sub categories, and then loop through your sub categories querying the posts for each. Like so:
$categories = get_categories( array ( 'child_of' => 10, 'orderby' => 'name' ) );
foreach( $categories as $category ){
// query_posts for category by $category->term_id
// Display posts for this category
}
Does that work for what you are wanting to do?

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