I am looking to implement a custom post query that includes the following parameters:
- WordPress custom loop to select the oldest post from a custom post type and display it on the page. There are multiple pages that will use this logic.
- Use PHP/WordPress logic to retrieve the second oldest post fourteen days later. This post would replace the oldest post on the page.
- After another fourteen days has passed, use the same logic to the retrieve the third oldest post. This logic should continue every fourteen days.
- The same logic with a slight variation should be used to populate an HTML dropdown menu. This menu will display all of the posts except for the one that is currently being displayed on the page as title, content, custom fields, etc.
- Advanced Custom Fields is installed and can be used in a meta_query if necessary.
Detailed Explanation
The client's website use pages with a custom template to display posts retrieved from the "sendouts" custom post type. These pages are referred to as "retailers" internally. Retailers will be added to the site at various times throughout the year. Each retailer is a business that will be providing send outs to their customers. The "sendout" posts will be a general pool that will be available to all of the retailers. New "sendout" posts will be added to WordPress every fourteen days by the client. When a new retailer page is created, the first post it will display will be the oldest. This post will display on the page. After fourteen days, the second oldest "sendout" post will be displayed on the page and the oldest will be moved into a HTML dropdown menu. This will continue every fourteen days for each retailer page.
The displaying of "sendout" posts will be unique to each page unless two pages were created on the same day. The page created date or use of Advanced Custom Fields could be used to provide the necessary parameters to achieve this functionality.
Base Code
$postDate = post_date_gmt('Y-m-d H:i:s');
$args = array(
'post_type' => 'sendouts',
'cat' => '101',
'order' => 'ASC',
'orderby' => 'date',
'date_query' => array(
'after' => 'post_date_gmt',
'before' => array(
// Possibly use: $postDate->add(new DateInterval('P14D')),
),
'inclusive' => true,
),
'posts_per_page' => -1,
);
query_posts($args);
Loop for HTML dropdown
<?php query_posts($args); ?>
<?php if (have_posts()) : ?>
<div class="sendout__nav-trigger">
<button class="btn btn--nav-trigger">
<span>Past Sendouts</span>
</button>
</div>
<ul class="sendout__list">
<?php if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>" rel="<?php the_ID(); ?>" class="sendout-link"><?php the_title(); ?></a>
</li>
<?php endwhile; endif; wp_reset_query(); ?>
</ul>
<?php else : ?>
<p>
<strong>Sorry, there are no retailer send outs to display.</strong>
</p>
<?php endif; ?>
My PHP knowledge is novice and my WordPress knowledge is midlevel, so I may be approaching this the wrong way.
query_posts
like the plague, never use that function.pre_get_posts
filter, orWP_Query
.query_posts
is a function you should not learn or use, it encourages bad habits, causes problems, reduces performances/site speed, and there are better alternatives. Consider its presence in WP legacy backwards compat$query
variable for your HTML dropdown created?