Multiple post loops in Wordpress - php

I've built a site for a health practitioner with CPT of:
(single-injuries.php, single-services.php, single-testimonials.php, single-partners.php)
and I have the appropriate (archive-injuries.php, archive-services, archive-testimonials, archive-partners) created with loops to display the relevant posts.
HOWEVER
I now want to create a sitemap page that pulls ALL THE POSTS from ALL ARCHIVES and just displays the PAGE NAME and URL for each...
How do I loop through multiple archives, do I nest a loop for each within a loop?

You can use a custom query that queries all of your CPTs which you listed (put them into the post_type array), similar to this (which lists all post titles found, each linked to its full post):
<?php
$args = array(
'post_type' => array('injuries', 'services', 'testimonials', 'partners' ),
'post_status' => 'publish',
);
$loop1 = new WP_Query($args);
if ( $loop1->have_posts() ) : while ( $loop1->have_posts() ) : $loop1->the_post();
$post_title = get_the_title();
?>
<div>
<p><a href='<?php echo get_the_permalink(); ?>'><?php echo post_title; ?></a></p>
</div>
<?php endwhile; else: ?>
<p>Nothing found</p>
<?php endif; ?>

I suggest you should do this using database, or what archives mean at your post? If you are using database just make a query that will select all from different tables, like this
SELECT archive-injuries.*, archive-services.*, archive-testimonials.*, archive-partners.* FROM your data base
Then make a while loop that will display posts, while mysqli_fetch_assoc have some data

Related

How to display all posts with the same title in wordpress single.php file

I have an anime website in WordPress where I add episodes as posts.
I'm using a theme that i created by myself, what i want is to display the list of episodes of the anime that the user is watching by looping through the posts with the same title to display them all using PHP. I tried a lot of solutions but none of them worked.
In brief, I want to display posts with the same title to make a list of episodes
This is one of the solutions that i tried and didn't work.
<div class="episodes">
<?php
$EpisodesList = new WP_Query('post_title='the_title()'');
if ($EpisodesList->have_posts()) {
while(have_posts()) {
the_post();
echo the_title( );
}
}
?>
</div>
You must get the post type 'EpisodesList' in your WP Query. WP_Query is a class used in WordPress theming that accepts a variety of parameters to request and fetch posts around those parameters. The example below allows you to set a list of parameters, fetch the posts matching those parameters, and display the title and excerpt of the post on the website.
Code goes in your custom template file where you want to render the posts.
/**
* Setup query to show the ‘EpisodesList’ post type with ‘8’ posts.
* Output the title with an excerpt.
*/
$args = array(
'post_type' => 'EpisodesList',
'post_status' => 'publish',
'posts_per_page' => 8,
'orderby’ => 'title',
'order’ => 'ASC',
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
print the_title();
the_excerpt();
endwhile;
wp_reset_postdata();

Writing a loop to conditionally display a custom post type, checking for ACF fields and Event Calendar Pro events

I'm making a site for a cookery school, and I need help with writing a loop. The loop will display the types of classes they provide, as long as each of those types of classes has upcoming events. Any help would be really appreciated, as everything I've tried so far has failed.
I'm using the Advanced Custom Fields plugin as well as the Events Calendar Pro plugin from Modern Tribe. I'll refer to the post type created by the Events Calendar Pro plugin as 'events' from now on.
I have a custom post type called 'class-type' which I've set up in my functions.php file. Each post of this type is a type of class that the cookery school offers, for instance DIY Pizza Club, Street Food Mastercless etc. The single post view will have images and details of that class, with a loop of upcoming events of that category, with the categories being the class name.
The 'class-type' post type has an ACF field called 'class_age', which is a select menu with the options 'For Adults' or 'For Kids & Teens'. It also has an ACF field called 'class_to_display', which is a taxonomy field showing categories from the Tribe Events posts.
The cookery school has two locations, and each event has it's location set to one of these.
The loop I need help with will have 4 variations on four different pages, and once I have the loop working I can make these variations myself. The variations will be:
Bristol, For Adults
Bristol, For Kids & Teens
Cardiff, For Adults
Cardiff, For Kids & Teens
So using the first variation as an example, the loop will need to do the following:
Get posts of type 'class-type'.
For each post, check that the post's ACF field 'class_age' is 'For Adults'.
Get the value in the post's ACF field 'class_to_display' and store it in a variable.
Check for posts of type 'events' with that variable as their category and with 'Bristol' as their location.
If events exist that meet those criteria, display the 'class-type' post's ACF fields of 'class_main_image', 'class_title', and 'class_short_description'.
I hope that's clear, and feel free to ask if you have any questions. Again, I really appreciate anyone taking the time to help!
Here's what I have so far which goes up to step 3 above, and is tested and working (the echo in p tags is just to check it works):
<?php
$args = array(
'post_type' => 'class-type',
'meta_key' => 'class_age',
'meta_value' => 'For Adults'
);
$the_query = new WP_Query( $args );
if( $the_query->have_posts() ):
while ( $the_query->have_posts() ) : $the_query->the_post();
$cookery_class = get_field('class_to_display'); ?>
<p><?php echo $cookery_class->name; ?></p>
<?php endwhile;
endif;
wp_reset_query(); ?>
Much to my surprise, I nailed it! Here's the code that works, hope it helps somebody.
<?php
$args = array(
'post_type' => 'class-type',
'meta_key' => 'class_age',
'meta_value' => 'For Adults'
);
$the_query = new WP_Query( $args );
if( $the_query->have_posts() ):
while ( $the_query->have_posts() ) : $the_query->the_post();
$cookery_class = get_field('class_to_display');
$cookery_class = (str_replace(' ', '-', strtolower($cookery_class->name)));
$check_has_events = tribe_get_events( array(
'tribe_events_cat' => $cookery_class,
'venue' => 59
)
);
if($check_has_events) {
$image = get_field('class_main_image'); ?>
<div class="cookery-class">
<?php if( !empty($image) ): ?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
<?php endif; ?>
<h3><?php the_title(); ?></h3>
<p><?php the_field('class_short_description'); ?></p>
</div>
<?php }
endwhile;
endif;
wp_reset_query(); ?>

How can I show a certain post recognised by title on my archive page? - Wordpress

So I basically have some sort of list with certain menu items, I want the top menu item to show on the archive page so it doesn't look empty. The post is always on top so I think the easiest way is to pick it out by the title of the post. But I have no idea how i can do this.
<?php
$query = array (
'post_type' => 'results',
'order' => 'DESC'
);
$queryObject = new WP_Query($query);
if ( $queryObject->have_posts() ): while ( $queryObject->have_posts() ): $queryObject->the_post(); ?>
<?php the_title(); ?>
<?php endwhile; endif; ?>
This is my code basically and you can see that it posts the titles from all posts in links so that is is some sort of menu, on click it shows the content from the post.

Wordpress custom post type menu generation

The problem: I have a site (under development) where I have generated a custom post type called "Products" . Inside I have different categories and posts. I would like to generate a menu like behaviour to my sidebar, but using google I haven't really stumbled onto something good. Who knows, maybe my idea is all wrong and is doable using some other solution.
Currently I have used WP Query to echo out all posts under one category. So this means that I have to manually copy code to echo new category with its posts when it is created. One solution is to create a menu manually, but i'd like to keep it auto-generated because users are not keen to do extra work and ofter forgot how it is done.
So, the question: Is my solution all wrong? How can i achieve menu like behaviour with class active for clicked category and its sub item? Is it better to just use pages?
Example of my code:
<?php
$args = array(
'post_type' => 'products',
'posts_per_page' => -1,
'cat' => 1
);
$query = new WP_Query( $args );
if ($query->have_posts()) {
echo '<ul>';
while ( $query->have_posts() ) : $query->the_post(); ?>
<li>Category name, title</li>
<li class="cpt-menu-item" id="post-<?php the_ID(); ?>">
<?php the_title(); ?>
</li>
<?php endwhile;
echo '</ul>';
}
wp_reset_postdata();
?>

How can I get all records of taxonomy category in WordPress Query?

I have use easy content Types plugin & created Post type in WP call Recipes. I have also added a taxonomy category in it & created 4 categories like Starter, Drinks, etc etc.
Now in WP query I need to get all records of starter.
So how can I get that?
I am using this query, but it is not working. It is giving all records of Recipes post type
Here is Query
$recipes = query_posts('post_type=recipes&taxonomy=recipescategory&category_name=Starters');
You have a lot of errors in your code and a misunderstanding about categories.
Never use query_posts to construct a custom query
Note: This function isn't meant to be used by plugins or themes. As explained later, there are better, more performant options to alter the main query. query_posts() is overly simplistic and problematic way to modify main query of a page by replacing it with new instance of the query. It is inefficient (re-runs SQL queries) and will outright fail in some circumstances (especially often when dealing with posts pagination)
If you have to run a custom query, make use of WP_Query or get_posts
category_name takes the category slug, not the name. The parameter's name is deceiving
The "categories" belonging to a custom taxonomy are called terms. I have written a post which I have also included in the codex which you can check here, it describes the differences.
To retrieve posts from a custom taxonomy, you need to make use of a tax_query. The category parameters will not work here
With all the above said, create your query so that it looks like this
$args = array(
'post_type' => 'recipes',
'tax_query' => array(
array(
'taxonomy' => 'recipescategory',
'field' => 'name',
'terms' => 'Starters',
),
),
);
$query = new WP_Query( $args );
if( $query->have_posts() ){
while( $query->have_posts() ) {
$query->the_post();
//Your loop elements
}
wp_reset_postdata();
}
try it
$ar = array (
'post_type'=>'recipes',
'taxonomy'=>'recipescategory',
'category_name'=>'Starters'
);
$posts = get_posts($ar);
** foreach loop**
foreach($posts as $p){ ?>
<div class="sub_cont">
<div class="sub_img">
<?php $url = wp_get_attachment_url( get_post_thumbnail_id($p->ID));?>
<img src="<?php echo $url; ?>" longdesc="URL_2" alt="Text_2" />
</div>
<div class="desc_title">
<a href="<?php echo $permalink = get_permalink( $p->ID); ?>">
<?php echo $post_title=$p->post_title; ?>
</a>
</div>
<div class="cont_add"></div>
</div>
<?php } ?>
You can use get_posts function
$args = array("post_type"=>"recipes","category_name"=>"starter","posts_per_page"=>20);
$starters = get_posts($args);

Categories