WP Offset Not Working with Foreach loop - php

I'm currently working a wordpress loop to retrieve blog posts, their titles, featured image, date and category. With that said, I'm attempting to offset the loop begin on the 5th descending post because the previous 4 are referenced earlier on the page.
I have successfully offset the posts but it seems that I can't grab the category.
<?php
$post_args = array(
'post_type' => 'post',
'post_status' => 'publish',
'order' => 'DESC',
'offset' => 4
);
$post_query = new WP_Query($post_args);
if ($post_query->have_posts() ):
$count = 1;
$terms = get_terms( array(
'taxonomy' => 'category',
'hide_empty' => true
) );
while ( $post_query->have_posts() ) : $post_query->the_post();
$feat_img = wp_get_attachment_url( get_post_thumbnail_id() );
?>
<div class="col-sm-3 col-xs-6">
<div class="featured-img" style="background-image: url(<?php echo $feat_img; ?>)"
<?php the_date('F j Y', '<h6>', '</h6>'); ?>
<h3><?php the_title(); ?></h3>
<div class="category"><?php echo $terms->name; ?></div>
</div>
</div>
I tried a slightly different approach and was able to get each posts category using a foreach loop, followed by a while and if loop. While I successfully got each posts category, the offset wasn't cooperating. Perhaps I'm overthinking it. Here's my other attempt at this.
<?php
$terms = get_terms( array(
'taxonomy' => 'category',
'hide_empty' => true,
) );
$count = 1;
foreach ( $terms as $term ) :
$post_args = array(
'offset' => 4,
'post_type' => 'post',
'order' => 'DESC',
'post_status' => 'publish',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => $term->slug
)
),
);
$post_query = null;
$post_query = new WP_Query($post_args);
if ( $post_query->have_posts() ) :
while ($post_query->have_posts() ) : $post_query->the_post();
$feat_img = wp_get_attachment_url( get_post_thumbnail_id() );
?>
Anyone mind lending a hand to help accomplish both tasks? Any input would be greatly appreciated. Thank you in advance.

you need to set "posts_per_page" to some other value than -1, it's explained well in documentation
https://codex.wordpress.org/Class_Reference/WP_Query
posts_per_page (int) - number of post to show per page (available
since Version 2.1, replaced showposts parameter). Use
'posts_per_page'=>-1 to show all posts (the 'offset' parameter is
ignored with a -1 value). Set the 'paged' parameter if pagination is
off after using this parameter. Note: if the query is in a feed,
wordpress overwrites this parameter with the stored 'posts_per_rss'
option. To reimpose the limit, try using the 'post_limits' filter, or
filter 'pre_option_posts_per_rss' and return -1

Related

Custom Shortcode w/ Custom $tax_query Attribute

I'm trying to create a custom shortcode in Wordpress that will let me place an attribute to specify a category and query within a custom post type called "case_studies". The taxonomy itself is called "case_study_categories" and I've set the default to be "furniture-interior", as you can see.
What I need is for the shortcode to look something like this:
[get_project_gallery order="ASC" tax_query="flooring"]
Ideally, the shortcode attribute I've placed would override the default of "furniture-interior" and instead use "flooring" to display the appropriate posts. Actually, I would think I don't even really need a default defined at all (so it would just get all the case_studies posts if no attribute is specified) but I can't get that to work either.
BTW, the attribute for order works exactly the way I would want - it overrides the defined default. I just can't figure out how to get this done with the $tax_query.
Any help on this would be very much appreciated. Thanks in advance all!
Here's my code:
function project_gallery_function($atts, $content = null) {
extract(shortcode_atts(array(
'order' => 'DESC',
'orderby' => 'date',
'tax_query' => array(
array(
'taxonomy' => 'case_study_categories',
'field' => 'slug',
'terms' => 'furniture-interior'
)
)
), $atts));
$args = array(
'post_type' => 'case_studies',
'post_status' => 'publish',
'posts_per_page' => '4',
'order' => $order,
'orderby' => $orderby,
'tax_query' => array($tax_query)
);
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
echo '<aside class="grid-gallery four-col grid-gap-4">';
while ( $the_query->have_posts() ) {
$the_query->the_post();
$gallery_thumb = get_the_post_thumbnail_url( $page->ID, 'thumbnail' );
echo '<img src="'. $gallery_thumb .'" alt="" />';
}
echo '</aside>';
}
/* Restore original Post Data */
wp_reset_postdata();
}
add_shortcode( 'get_project_gallery', 'project_gallery_function' );
You shouldn't be worrying about the whole entire tax_query unless you plan on actually redoing the whole tax_query. You should consider changing it to just using $terms instead, since that's the only part you're replacing with the shortcode (you don't need to rebuild the whole array).
Secondly, make sure your code is normalized with your spacing and indentation - future you appreciates the forethought!
Lastly, you could consider passing even more variables to the array to make it a bit more extensible - $posts_per_page, $taxonomy etc.
function project_gallery_function( $atts, $content = null ){
extract( shortcode_atts( array(
'terms' => 'furniture-interior',
'field' => 'slug',
'order' => 'DESC',
'orderby' => 'date',
'taxonomy' => 'case_study_categories',
'post_type' => 'case_studies',
'posts_per_page' => 4,
), $atts ) );
$tax_query = array(
array(
'taxonomy' => $taxonomy,
'field' => $field,
'terms' => explode(',', preg_replace('/\s+/', '', $terms)), // Force to array, allowing comma sep values
)
);
$args = array(
'post_type' => $post_type,
'post_status' => 'publish',
'posts_per_page' => $posts_per_page,
'order' => $order,
'orderby' => $orderby,
'tax_query' => array( $tax_query )
);
// The Query
$the_query = new WP_Query( $args );
// The Loop
if( $the_query->have_posts() ){
echo '<aside class="grid-gallery four-col grid-gap-4">';
while( $the_query->have_posts() ){
$the_query->the_post();
if( $gallery_thumb = get_the_post_thumbnail_url( $page->ID, 'thumbnail' ) ){
printf( '<img src="%s" alt="" />', get_the_permalink(), $gallery_thumb );
}
}
echo '</aside>';
}
/* Restore original Post Data */
wp_reset_postdata();
}
add_shortcode( 'get_project_gallery', 'project_gallery_function' );
A few other notes, get_the_post_thumbnail_url() returns a falsey value if it doesn't exist, so you should wrap that in an if statement incase the image can't be found for some reason (corrupted, removed, forgot to add, etc). Also you have a $page->ID variable in get_the_post_thumbnail_url() - but don't seem to have a global $page in the function? Make sure that's the variable you want to use there.
Using code like what I've provided will let you do what you want, [get_project_gallery order="ASC" terms="flooring"], or even add multiple [get_project_gallery order="ASC" terms="flooring,something-else,a-third-thing"] - just match up the shortcode attributes with the parameter you want to override: [get_project_gallery order="ASC" terms="flooring,something-else,a-third-thing" posts_per_page="15"]

WordPress: Category sort by Latest Post Date

I am working in a custom WP theme.I need to show each posts under individual categories, which is working fine.But, i want to sort the categories according to the published date of posts.
I found one answer, but its not working in my code and i cant figure how to use it in my code.So, i need anyone's expert help/input in below code.
Similar answer
**<?php
$terms = get_terms( array(
'taxonomy' => 'category',
'hide_empty' => false,
'orderby' => 'date',
'order' => 'ASC',
) );
foreach($terms as $cat){
$cata_name = $cat->name;
$term_id = $cat->term_id;
$catqueryy = new WP_Query( 'cat='.$term_id.'&posts_per_page=10');$count = $catqueryy->found_posts;while($catqueryy->have_posts()) : $catqueryy->the_post();?>
<div class="list-group">
<p class="post_excerpt">
<?php echo ' '.__(get_the_title(),'rockon').''; ?>
</div>
<?php
endwhile;
?>
You can try this to show all categories by date
<?php $args = array(
'show_option_all' => '',
'orderby' => 'ID',
'order' => 'DESC',
); ?>
<?php wp_list_categories( $args ); ?>

Wordpress - Increase amount of posts to be shown within specific category

I've been running in to a problem.
I'm editing a custom wordpress theme. I want to make some adjusments to the FAQ page (created with custom post type).
Right now every subject of question (= also category) is showing only 5 answers (these are posts).
I wanted to know, how I could increate this number, so instead of 5, show 10 or rather, show all answers per subject.
This is the code:
archive-faq.php
<?php $terms = get_terms( 'faq_category' ); ?>
<?php foreach( $terms as $term ):
$args = array (
'post_type' => 'faq',
'tax_query' => array(
array(
'taxonomy' => 'faq_category',
'field' => 'id',
'terms' => $term->term_id,
),
),
);
$query = new WP_Query( $args ); ?>
<h1><?php echo $term->name; ?></h1>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<div class="toggle-holder">
<h5 class="toggle"><span><?php the_title(); ?></span></h5>
<div class="toggle-box">
<div>
<?php the_content(); ?>
</div>
</div>
</div>
<?php endwhile; ?>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
An important part is:
<div class="toggle-box">
<div>
<?php the_content(); ?>
</div>
</div>
Where the_content() , is showing the posts that are in a certain category.
However it's nowhere to be found why , the page is only showing up to 5 posts (answers) and not any more ?
Things I've tried:
$query = new WP_Query( $args );
$query_posts('post_per_page=3'); ?>
Also:
putting 'showposts' => 8 under 'terms' => $term->term_id,
And I also tried:
$query = new WP_Query( $args ); ?>
<?PHP
query_posts( array(
'workcap' => $all_post_terms,
'showposts' => 8,
'caller_get_posts' => 1,
'post__not_in' => $do_not_duplicate ) ); ?>
->> In summary:
Why does the page only show up to 5 posts ?
And how do I change this property ?
PS. If you want to see the page: http://goo.gl/UnWRTz
The argument is posts_per_page and not post_per_page. Its a typo?
Try this:
$args = array (
'post_type' => 'faq',
'posts_per_page' => -1,
//for now try without this taxonomy, if it works try with it.
/*'tax_query' => array(
array(
'taxonomy' => 'faq_category',
'field' => 'id',
'terms' => $term->term_id,
),
),*/
);
Btw, you can find more info here: https://codex.wordpress.org/Class_Reference/WP_Query

Having trouble with wordpress custom taxonomy archive

I'm having a little trouble with a custom taxonomy template. I inherited a site that was developed by someone else and they use "Types" plugin to add some custom taxonomies.
Goal:
to have an archive template that shows only posts with a certain taxonomy term in it at example-domain.com/people/harrison-ford
Problem:
This code is bringing in posts that do not have the taxonomy selected.
Here's my full code:
<?php
$year = get_post_meta($post->ID, 'year', true);
$post_type = 'post';
$tax = 'people';
$tax_terms = get_terms( $tax );
if ($tax_terms) {
$args = array(
'post_type' => $post_type,
'people' => 'harrison-ford',
"$tax" => $tax_term->slug,
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1,
'orderby' => 'date',
'order' => DESC
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) : ?>
<h2 class="wwNews"><?php echo $tax_term->name; ?> News</h2>
<?php while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
<-- display stuff -->
<?php endwhile; // end of loop ?>
<?php endif; // if have_posts()
wp_reset_query();
}
?>
What are you expecting here? "$tax" is going to to be 'people' =>, which is going to overwrite 'harrison-ford' to the value of $tax_term->slug.
'people' => 'harrison-ford',
"$tax" => $tax_term->slug,
Furthermore, I don't know of any custom argument called people, I'm pretty sure that you want tax_query:
'tax_query' => array(
'taxonomy' => 'people',
'terms' => array('harrison-ford', $tax_term->slug)
)
Which will give you the results of all people matching harrison-ford and the value of $tax_term->slug within the taxonomy of people

Limit posts with attachment inside Wordpress custom post type query

I´m trying to get the post thumbnail and another attached image from the last post of a custom post type (called parceiros-e-links).
What I got is to get all posts with images and show them... but I need to show only the last post and show it with two images (the_post_thumbnail and the wp_get_attachment_image)
Here is my current code:
<?php
$query = new WP_Query( array( 'post_type' => 'parceiros-e-links', 'posts_per_page' => -1, 'orderby' => 'date', 'order' => 'DESC' ) ); //the first loop where I filter the posts from custom post type and by date
if( $query->have_posts() ){
while($query->have_posts()){
$query->the_post();
$image_query = new WP_Query( array( 'post_type' => 'attachment', 'post_status' => 'inherit', 'post_mime_type' => 'image', 'posts_per_page' => 2, 'post_parent' => get_the_ID() ) ); //the second loop where I filter the posts with attachments and limit to two
while( $image_query->have_posts() ) { $image_query->the_post();
//code below prints the two attachments: thumbnail and another one.
if(has_post_thumbnail()){
the_post_thumbnail('home-parceiros');
}
echo wp_get_attachment_image( get_the_ID(), 'home-parceiros-foto' );
}
}
}
?>
I already spent many hours searching similar situations and trying to filter this code but I'm without ideas... Because if I limit the query's first posts_per_page to 1, if the most recent post doesn't has an attachment, no images will be printed!
Any clues about how to limit the number of posts with attachments inside a custom post type?
Thanks!
Your mistakes was at the_post_thumbnail('home-parceiros'); and echo wp_get_attachment_image( get_the_ID(), 'home-parceiros-foto' ); Read about the correct attributes given for this two functions here:
http://codex.wordpress.org/Function_Reference/the_post_thumbnail
http://codex.wordpress.org/Function_Reference/wp_get_attachment_image
Here is my suggestion:
$query = new WP_Query(
array(
'post_type' => 'parceiros-e-links',
'posts_per_page' => 1,
'orderby' => 'date',
'order' => 'DESC'
)
);
if($query->have_posts()) :
while($query->have_posts()) : $query->the_post();
if(has_post_thumbnail()) : the_post_thumbnail(); endif;
$args = array(
'post_type' => 'attachment',
'numberposts' => 1,
'post_status' => null,
'post_parent' => $post->ID
);
$attachments = get_posts( $args );
if ( $attachments ) {
foreach ( $attachments as $attachment ) {
echo wp_get_attachment_image( $attachment->ID, 'full' );
}
}
endwhile;
endif;
Please let me know :)
Example #2 updated:
$query = new WP_Query( array( 'post_type' => 'parceiros-e-links', 'posts_per_page' => 1, 'orderby' => 'date', 'order' => 'DESC' ) ); //the first loop where I filter the posts from custom post type and by date
if( $query->have_posts() ){
while($query->have_posts()){
$query->the_post();
$image_query = new WP_Query( array( 'post_type' => 'attachment', 'post_status' => 'inherit', 'post_mime_type' => 'image', 'posts_per_page' => 2, 'post_parent' => get_the_ID() ) ); //the second loop where I filter the posts with attachments and limit to two
while( $image_query->have_posts() ) { $image_query->the_post();
//code below prints the two attachments: thumbnail and another one.
if(has_post_thumbnail()){
the_post_thumbnail('home-parceiros');
}
echo wp_get_attachment_image( get_the_ID(), 'home-parceiros-foto' );
}
}
}

Categories