WordPress wp_query featured posts - php

I'm working on a WordPress site, and I'm working on a requested feature from my client. The client would like to be able to feature certain posts, so that they appear at the top of a list. To make it easy for my client, I created a featured category. So when the client wants to feature a post, all they have to do is to add the featured category to the post.
Here is my current query to display all posts with the category name of events:
$query = new WP_Query( array( 'category_name' => array('events'), 'posts_per_page' => '1' ) );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
$postLink = get_permalink();
echo '<li class="wp-post">
<h5>'.get_the_title().'</h5>
<p>'.get_the_excerpt().'</p>
<div class="wp-post-details">
<span class="post-date">'.get_the_date().'</span>
</div>
</li>';
}
echo '<li>View All Events</li>';
wp_reset_postdata();
} else {
echo '<li>No Posts Found</li>';
}
I would like to change it, so it would display events posts that have the additional category of featured first. I have done some searching on Google and here. But as of yet, I have not found a solution that works for my instance.

I found the an solution to my issue. I just had to add if (in_category('feature')) {... before I echo the HTML.

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(); ?>

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();
?>

Show a single post on other template/page

Working on a site, and I need to have a separated template/page for showing just that one post.
On the home page (index) Ill loop through my categories and showing just a part of them - events - news - references.
Like this:
<?php
$query = new WP_Query( array( 'category_name' => 'Events' ) );
if ($query->have_posts())
{
while ($query->have_posts()) : $query->the_post();
$haveEvents = true;
if ($eventCount < 3) {
$eventCount++;
?>
<div class="event-tile">
<div class="event-tile-content">
<h3><?php the_title(); ?></h3>
<h4><?php the_time('F jS, Y'); ?></h4>
Lees meer
</div>
</div>
<?php
}
endwhile;
}
Every section has a read all and read specific item button:
I already found out how to display all posts within a category on a separated page named: page-posts.php
On page-posts.php I simply check on which page I am and depending on which page, make a query to show the posts within a specified category
$query;
if (is_page('events')) {
$query = new WP_Query( array( 'category_name' => 'Events' ) );
}
elseif (is_page('news')) {
$query = new WP_Query( array( 'category_name' => 'News' ) );
}
if ($query->have_posts())
{
etc..
Now what I cant figure out (not even after spending like hours on google), how to show a single post from the home page (index.php), on a single-post page...
On the action buttons I use:
Lees meer
Which brings me back to the homescreen. (just started on Wordpress)
In a Wordpress theme single.php is used to display a post. If it does not exist it drops down to using your index.php
Try moving your page-posts.php to single.php
Maybe its better to use archive-events.php. You can make an archive for custom posts.
With that you can do: where events is your custom post-type. Then you land on the events archive page.
As far as i can see your problem is that the post is the global post (from your home). So maybe you can setup_postdata($post), or use the home_url('url') functions.

wordpress combine page and posts

I am creating a wp template based on 2013. I want to display a 'page' that has some content from a page and then 6 posts on it. These posts have been selected via the themes options panel using the settings api. So I can get at each one using
$options = get_option( 'osc_theme_options' );
The template i have been using so far is based on a page so i am guessing I need to change the loop in someway.
The loop goes:
<?php /* The loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>...
I want to know how to alter the loop so that it pulls in just the posts that have been selected. At the moment this template/loop is only pulling in the page - which I guess is fair enough.
Would it be possible to create 'another loop' maybe under the first that then brings in the selected posts - if so how?
Many thanks
Yes you can effectively create another loop under the existing loop to display the posts. I am not sure what $options = get_option( 'osc_theme_options' ); returns, i.e an array of Post ID's etc. In order to show the posts you need to do a custom loop:
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() )
{
echo '<ul>';
while ( $the_query->have_posts() )
{
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
}
else
{
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
This is taken from:
https://css-tricks.com/snippets/wordpress/custom-loop-based-on-custom-fields/
Also see the following:
https://digwp.com/2011/05/loops/
http://www.smashingmagazine.com/2013/01/14/using-wp_query-wordpress/
So effectively it all boils down to the $args variable as to which posts you will get. Here is an example of multiple ids
id=2,6,17,38
So if $options = get_option( 'osc_theme_options' ); returns an array of post ids like so:
array(
0=>1
1=>22
2=>27
)
You could probably do something like:
$args = "id=".implode($options,",");
This is the best advice I can give without deeper knowledge of the theme etc.

Categories