Wordpress Loop: Insert Content After Specific Post Number - wordpress

I am wondering if there is a way to insert a block of content after a specific post number in my Custom Post Loop.
Essentially, I want to say:
After the 2 most recent mosts, insert this block of code.
If that can be done, I would also be interested in knowing if something like this is achievable:
Start a Custom Post Loop that excludes a specific Post Format (or Category), but after every 2 posts, insert 1 of the excluded Post Formats.
Thanks for any assistance.
<!-- #STICKY-POSTS |begin| -->
<section id="sticky-posts">
<?php
$args = array( 'post_type' => 'portfolio', 'posts_per_page' => 2, 'cat' => '8' );
$custom_query = new WP_Query( $args);
while($custom_query->have_posts()) : $custom_query->the_post();
get_template_part('format', 'standard');
endwhile;
?>

Do something like this:
<!-- #STICKY-POSTS |begin| -->
<section id="sticky-posts">
<?php
$args = array( 'post_type' => 'portfolio', 'posts_per_page' => 2, 'cat' => '8' );
$custom_query = new WP_Query( $args);
$x = 0;
while($custom_query->have_posts()) : $custom_query->the_post();
if($x==2) {
display your thing here.
$x=0;
}
get_template_part('format', 'standard');
$x++
endwhile;
?>
Unless I botched something, x=0 will turn 1 at first normal post, 2 at second normal post then return true on the if statement, got back to 0 and rinse repeat.

Related

Sticky posts not showing when using category filter

I am trying to show a specific category post on my home page and in which 1 post is sticky that will appear first but its not working.
I have noticed when I try to show all posts, then sticky post shows first. When I try to show a specific category then its not showing first.
Here is my code:
$sticky = get_option( 'sticky_posts' );
$args = array( 'posts_per_page' => intval($blogtoShow),
'post_status'=>'publish',
'post_type'=>'post',
'cat' => $cattoShow,
'orderby'=>'date',
'post__in' => $sticky);
$the_query = new WP_Query( $args );
if ($the_query->have_posts()) :
while( $the_query->have_posts() ) : $the_query->the_post();```
You're looking for the following:
ignore_sticky_posts (boolean) – ignore post stickiness (available since version 3.1, replaced caller_get_posts parameter). false (default): move sticky posts to the start of the set. true: do not move sticky posts to the start of the set.
If you're using a pre-made theme the default might have been modified.
You should add this to your arguments array: 'ignore_sticky_posts' => 0.
A comma should separate each arguments. (Not tested but should work)
More info on the worpress query: https://developer.wordpress.org/reference/classes/wp_query/
----------
EDIT 1.1: I think you need to display a specific template for the sticky (as it's not considered a normal post). At the begining of your loop can you try the following?
$sticky = get_option( 'sticky_posts' );
$args = array(
'posts_per_page' => 3,
'post__in' => $sticky,
);
$query = new WP_Query( $args );
if ( $sticky[0] ) {
// insert sticky template...
} else {
// insert posts template...
}

WordPress - get the 4th post in a query

I have a WordPress query like this :
<?php $recent = new WP_Query( array( 'tag' => $tags, 'posts_per_page' => '4' ) ); while( $recent->have_posts() ) : $recent->the_post(); ?>
Which gives me the last 4 posts using the $tags.
But any idea of how may I edit this code to do not get the four first posts but from the 4th?
Thanks!
Take a look at the WP_Query Documentation. It goes into detail on how to properly interact with the class.
If you want to query your posts starting from the 4th post, you'll want to take a look at the offset parameter. In your case, take a look at the code below (Note: I moved the arguments array to a variable for clarity)
$recent_args = array(
'tag' => $tags,
'posts_per_page' => 4, // Don't need quotes around integers
'offset' => 3, // Add this param to "Skip" this many posts
);
$recent = new WP_Query( $recent_args );
// Loop through your posts here
If you need to get all posts beyond the first (newest) posts, you can use the offset argument:
<?php $next_posts = new WP_Query( array(
'tag' => $tags,
'offset' => 4,
)
);
while($next_posts->have_posts()) : $next_posts->the_post(); ?>
There is a caveat to this! If you need pagination or set posts_per_page to -1, this will not work. For more information on a more robust solution if you need pagination, checkout the WP documentation:
https://codex.wordpress.org/Making_Custom_Queries_using_Offset_and_Pagination
So here is the code that worked for me :
<?php $recent = new WP_Query(array( 'tag' => $tags, 'posts_per_page' => '4', 'offset' => '3', )); while($recent->have_posts()) : $recent->the_post(); ?>
Thanks a lot to #disinfor & #Xhynk for leading me to the right direction !

WordPress Adding Variables to Loop Arguments

I have a custom post-type call Called 'Sectors' and another post type called 'Challenges' The challenges post type has a taxonomy called 'sectortype' - which has the same names as the sectors.
I created a page called 'single-sector.php' On that page displays a loop that includes challenges related to that sector.
When I write the loop for displaying challenges, how do I make the 'sectortype' => 'advanced-education' a variable so it will work on other single sector pages?
Here's what I have for the loop...
<?php $challenge_args = array(
'post_type' => 'challenge',
'sectortype' => 'advanced-education', //Need Help Here
);
// create a new instance of WP_Query
$challenge_query = new WP_Query( $challenge_args );
?>
<?php if ( $challenge_query->have_posts() ) : while ($challenge_query->have_posts() ) : $challenge_query->the_post(); // run the loop ?>
Get Custom posts by custom taxonomy terms :
<?php
$terms = get_terms('sectortype');
$challenge_args = array(
'post_type' => 'challenge',
'publish_status' => 'published',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'sectortype',
'field' => 'slug',
'terms' => $terms[0], //whichever term you want to select
),
),
);
// create a new instance of WP_Query
$challenge_query = new WP_Query( $challenge_args );
?>
<?php if ( $challenge_query->have_posts() ) : while ($challenge_query->have_posts() ) : $challenge_query->the_post(); // run the loop ?>
DISPLAY IN SEPARATE PAGES
TO display the posts in separate pages as you mentioned in the comment, you have to do the following:
Create Separate Page Links:: (use on page as navigation items)
<?php $categories = get_terms('sectortype');?>
<ul>
<?php foreach( $categories as $key => $c ):?>
<?php $cat_link = get_term_link( $c->term_id );?>
<?php $term_title= single_term_title('', false);?>
<li class="<?php echo ($c->name == $term_title )?'active':'';?>"><?php echo $c->name;?></li>
<?php endforeach;?>
</ul>
Create a file in theme directory (actually an archive template for taxonomy terms) with the filename 'taxonomy-sectortype.php'.
On that template, get the posts from the usual loop without using any queries and you will get the respective posts.

Let users sort posts in Wordpress

I’d like to create a page for filtering posts based on a number of criteria.
I can work with wp_query and deliver posts quite easily, my problem is that I can’t figure out (nor can I find any answers online about this, believe me I looked) how to let users do this.
Take this for example, returns the posts in order of price (custom field meta value) from highest to lowest with 33 posts.
<?php
$featuredPosts = new WP_Query( array(
'posts_per_page' => 33,
'meta_key'=>'Price',
'orderby' => 'meta_value_num',
'order' => DESC
) );
?>
<?php if ( $featuredPosts->have_posts() ) : ?>
<?php while ( $featuredPosts->have_posts() ) : $featuredPosts->the_post(); ?>
<article <?php post_class('item-post block'); ?> id="post-<?php the_ID(); ?>">
<h2 class="price-title"><?php the_title(); ?> </h2>
</article> <!-- end div post -->
<?php endwhile; wp_reset_query(); ?>
<?php endif; ?>
Now, even after reading and googling, I’ll be damned if I can figure out how I’d implement this on the front end for users to filter posts.
I mean, I know you can append to the URLs in Wordpress to alter the order of posts, but in this context I’m totally lost.
I tried this, but it doesn't work.
<?php
$by_price = esc_url(add_query_arg(array(
'meta_key' => 'price',
'orderby' => 'meta_value_num',
'order' => ASC
)));
$by_date = esc_url(add_query_arg(array(
'orderby' => 'date',
'order' => DESC
)));
?>
<ul>
<li>Order by price</li>
<li>Order by date</li>
</ul>
What I’m trying to achieve is actually quite simple as well, let the user choose the category, choose the price range (guessing I’d write something in JQuery to deliver a value into an field), set the number of results they’d like to be returned.
I’ve tried googling everything under the sun I can think of for this, no dice.
Try Simple Custom Post Order plugin.
It is using AJAX and JavaScript, you don’t have to load anything else. You have to just drag and drop the posts.
OK, I update the code to make it clear:
---I do not think meta_key would be auto-pickup---
functions.php
...
$whitList = array(
'price' => array(
'posts_per_page' => 33,
'meta_key'=>'price',
'orderby'=>'meta_value_num',
'order' => ASC
),
'date' => array(
'posts_per_page' => 33,
'orderby'=>'date',
'order' => DESC
)
);
...
Your first loop php:
<?php
gloabl $whitList; //to use the $whitList in your functions.php.
$aryQuery = $whitList[$_REQUEST['orderby']] ? $whitList[$_REQUEST['orderby']] : $whitList['price'];
$featuredPosts = new WP_Query( $aryQuery );
....
....
?>
For your list page:
<ul>
<?php
gloabl $whitList; //to use the $whitList in your functions.php.
foreach( $whitList as $orderby => $aryOrderBySettings){
?>
<li> Order by <?php echo $orderby;?></li>
<?php
}
?>
</ul>
Using $_GET parameters is the way to go here. First of all you'll want to allow your visitors access to these add these variables. The link approach is fine, overall, so we can generate augmented links by using the add_query_arg to tack on extra parameters to the current URL.
<?php
$urla = add_query_arg( 'sort' => 'price', 'asc' => '1' );
$urld = add_query_arg( 'sort' => 'price', 'asc' => '0' );
?>
Sort by price (asc)
Sort by price (desc)
When clicked, the tacked on variables can thus be detected:
<?php
// Get an allowed sort variable and the order
$sort = isset( $_GET['sort'] ) && in_array( $_GET['sort'], array( 'price' ) ) )
? $_GET['sort'] : null;
$order = isset( $_GET['asc'] ) && $_GET['asc'] == '0' ? 'DESC' : 'ASC';
?>
Now you would augment your main query with the data you just retrieved. If you're using the default way of querying posts on a page you should be able to get away with query_posts, although it is not recomended. And, if you're using a custom loop, simply inject the new arguments into it:
<?php
$args = array();
switch ( $sort ):
case 'price':
$args['order'] = $order;
$args['orderby'] = 'meta_value_num';
$args['meta_key'] = 'price';
break;
default:
break;
endswitch;
$defaults = array( 'posts_per_page' => 33 );
$query = new WP_Query( wp_parse_args( $args, $defaults ) );
?>
You can add more variables, by creating more URLs and buttons to press, and more cases in the switch statement to extend the basic example above.
The first piece of code would go wherever you want your buttons to appear. The second piece of code goes before the third one, which goes before outputting the results.

Order WP_Query by meta and post date

I'm currently working on a small blog which has a sidebar that displays all "special projects".
"Special projects" is just a category, the sidebar will only display 4 posts at a time which are filtered by post date, but I also have a custom meta box to allow the user to feature posts.
These featured post should show up on the top of the special projects.
Right now my query is like this:
new WP_Query("showposts=" . $instance['num'] . "&cat=" . $instance["cat"] . "&order=ASC&order_by=date")
And I can get the featured meta data like this:
$featured = get_post_meta($single_cat_post->ID, 'soy_featured_post', true);
But how could I integrate this inside the WP_Query?
First of all, 'showposts' has been replaced by 'posts_per_page' when using WP_Query. I've corrected that in your code. Also, within a loop you should just be able to use $post->ID instead of $single_cat_post->ID.
I would use two loops. Set your paramaters, then in the first loop include a condition to check for the meta value, reset the query, then do another loop and include a condition that checks for the meta value and outputs nothing if it exists.
In the first query, I added a check to see how many posts are returned by the first loop. Then using that value (subtracted by 4) I calculated a variable to use for posts_per_page in the second loop. Then I added a conditional to only run the loop if the result is greater then 0.
This is untested, but it should work or at least put you on the right path!
<?php
$args = array(
'posts_per_page' => 4,
'meta_key' => 'soy_featured_post',
'cat' => $instance["cat"],
'orderby' => 'date',
'order' => 'ASC'
);
$special_post_query = new WP_Query( $args );
$special_posts_found = $special_post_query->found_posts;
if ($special_post_query->have_posts()) :
while( $special_post_query->have_posts() ) : $special_post_query->the_post();
// POST WITH META VALUE OUTPUT
the_title();
endwhile;
endif;
wp_reset_query();
$second_loop_posts_per_page = 4 - $special_posts_found;
if ($second_loop_posts_per_page > 0) {
$args = array(
'posts_per_page' => $second_loop_posts_per_page,
'cat' => $instance["cat"],
'orderby' => 'date',
'order' => 'ASC'
);
if ($special_post_query->have_posts() ) :
while( $special_post_query->have_posts() ) : $special_post_query->the_post();
// Condition to test for NO meta value
if (get_post_meta($post->ID, 'soy_featured_post', true) == null) {
// CODE
the_title();
} else {
// Don't print anything because the meta value exists
}
endwhile;
endif;
wp_reset_query();
} ?>

Resources