Wordpress ACF plugin - how to order by date field - wordpress

I am using Advanced custom Fields plugin to create an event date in a custom post. I would like to order my events by that date, but it doesn't works for me:
<div class="column one">
<?php
$args = array(
'post_type' => 'formation',
'posts_per_page' => -1,
'post_status' => 'publish',
'meta_key' => 'date_formation',
'orderby' => 'meta_value_num',
'order' => 'DESC'
);
$posts_query = new WP_Query( $args );
while ($posts_query->have_posts()) : $posts_query->the_post();
$presentation = get_field('date_formation');
$date_formation = mysql2date( 'j F Y ', $presentation);
$lieu = get_field('adresse_formation');
?>
<div class="column one-third eventpost">
<div class="event-col">
<?php echo '<h4>'.$date_formation.''.$lieu.'</h4>'; ?>
<?php if( !is_single() ) {?><?php the_excerpt(); ?> <?php }else{ ?>
<?php the_content(); ?>
<?php } ?>
<p>En savoir plus<i class="icon-right-open"></i></p>
</div>
</div>
<?php endwhile; wp_reset_query(); ?>
</div>

Try to use Parham's solution from this question. You'll need to modify it a bit like so:
<div class="column one">
<?php
function wpse_130954_orderby_fix($pieces){
global $wpdb;
$pieces['where'] .= " AND $wpdb->postmeta.meta_key = 'date_formation'";
$pieces['orderby'] = "$wpdb->postmeta.meta_value DESC";
return $pieces;
}
add_filter( 'posts_clauses', 'wpse_130954_orderby_fix', 20, 1 );
$args = array(
'post_type' => 'formation',
'posts_per_page'=> -1,
'post_status' => 'publish',
);
$posts_query = new WP_Query( $args );
while ($posts_query->have_posts()) : $posts_query->the_post();
$presentation = get_field('date_formation');
$date_formation = mysql2date( 'j F Y ', $presentation);
$lieu = get_field('adresse_formation');
?>
<div class="column one-third eventpost">
<div class="event-col">
<?php echo '<h4>'.$date_formation.''.$lieu.'</h4>'; ?>
<?php if( !is_single() ) {?><?php the_excerpt(); ?><?php }else{ ?><?php the_content(); ?><?php } ?>
<p>En savoir plus<i class="icon-right-open"></i></p>
</div>
</div>
<?php endwhile; wp_reset_query(); ?>
<?php remove_filter( 'posts_clauses', 'wpse_130954_orderby_fix', 20 ); ?>
</div>

Related

post per page is not working with sticky post query in wordpress

this is my post loop that i am using
<?php
$sticky = get_option( 'sticky_posts' );
rsort( $sticky );
$args = array(
'post_type' => 'post',
'post__in' => $sticky,
'posts_per_page' => 1
);
$sticky_query = new WP_Query( $args );
while ( $sticky_query->have_posts() ) : $sticky_query->the_post();
?>
<article class="cust-arc-post">
<img src="<?php the_post_thumbnail_url(); ?>" alt="">
<div class="arc-post-header">
<h3><?php the_title(); ?></h3>
<a class="cat" href="javascript:;">Category Title</a>
</div>
<p><?php echo wp_trim_words( get_the_content(), 20, '...' ); ?></p>
</article>
<?php endwhile;
wp_reset_postdata();
?>
i tried using offset but no luck,
i think something is wrong with my loop
if anyone can help me with this
thanks in advance
All the information you need can be found in the theme handbook
As stated in the codex, this displays just the first sticky post, if none return the last post published:
$args = array(
'posts_per_page' => 1,
'post__in' => get_option( 'sticky_posts' ),
'ignore_sticky_posts' => 1
);
$query = new WP_Query( $args );
Your problem likely lies in the rsort(); function, because it's reversing the array from highest to lowest.
Try the below code.
<?php
$sticky = get_option( 'sticky_posts' );
rsort( $sticky );
$posts_per_page = 12;
$sticky_count = count($sticky);
if ($sticky_count < $posts_per_page) {
$posts_per_page = $posts_per_page - $sticky_count;
} else {
$posts_per_page = 1;
}
$args = array(
'post_type' => 'post',
'post__in' => $sticky,
'posts_per_page' => $posts_per_page
);
$sticky_query = new WP_Query( $args );
while ( $sticky_query->have_posts() ) : $sticky_query->the_post(); ?>
<article class="cust-arc-post">
<img src="<?php the_post_thumbnail_url(); ?>" alt="">
<div class="arc-post-header">
<h3><?php the_title(); ?></h3>
<a class="cat" href="javascript:;">Category Title</a>
</div>
<p><?php echo wp_trim_words( get_the_content(), 20, '...' ); ?></p>
</article>
<?php endwhile; wp_reset_postdata(); ?>

Wp query pagination and categories

I'm new to Wp and I'm trying to developing my first theme. After I add the paginations to my archive page the category filter doesn't work anymore (probably because I override the WP Query).
What shoud I put in the query in order to see only the posts of the category that is clicked? (if I don't put anything wp do the magic) but without the query the pagination doesn't work.
This is the archive.php
<?php
get_header(); ?>
<?php
if ( have_posts() ) : ?>
<header class="page-header no-image">
<?php
the_archive_title( '<h1 class="page-title u-text-center">', '</h1>' );
?>
</header><!-- .page-header -->
<?php get_template_part( 'template-parts/filter-category' ); ?>
<?php
/*QUERY:
This is the problem if I move the query from there the category works but
If I leave the query the pagination doesn't have the post_per_page argument to take and doesn't work...
*/
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'post_type'=>'post',
'post_status'=>'publish',
'posts_per_page' => 6,
'paged' => $paged
);
$wp_query = new WP_Query( $args );
/*END QUERY*/
?>
<div id="blogpost-list" class="container">
<div class="row blogpost-wrapper">
<?php
/* Start the Loop */
$i = 1;
while ( $wp_query->have_posts() ) : $wp_query->the_post();?>
<?php if ($i % 3 == 0): ?>
<div class="blogpost-item-grid">
<!-- some stuff to display the post.. doesn't really matter-->
<?php get_template_part( 'template-parts/content-post-preview-large' ); ?>
</div>
<?php else: ?>
<div class="blogpost-item-grid">
<!-- some stuff to display the post doesn't really matter -->
<?php get_template_part( 'template-parts/content-post-preview' ); ?>
</div>
<?php endif; ?>
<?php $i++; ?>
<?php endwhile;?>
</div>
</div>
<?php get_template_part( 'template-parts/pagination' ); ?>
<?php
else :
get_template_part( 'template-parts/content', 'none' );
endif; ?>
<?php
get_footer();
Here the code that I use for the pagination:
<?php //Require a wp->query ?>
<div class="container-fluid">
<div class="pagination-wrapper">
<?php
$pag_args = array(
'base' => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
'total' => $wp_query->max_num_pages,
'current' => max( 1, get_query_var( 'paged' ) ),
'format' => '?paged=%#%',
'show_all' => false,
'type' => 'plain',
'end_size' => 2,
'mid_size' => 1,
'prev_next' => true,
'prev_text' => sprintf( '<img class="icn icn-small" src="' . get_template_directory_uri() .'/assets/images/icn-chevron-left.svg"/>'),
'next_text' => sprintf( '<img class="icn icn-small" src="' . get_template_directory_uri() .'/assets/images/icn-chevron-right.svg"/>'),
'add_args' => false,
'add_fragment' => '',
);
echo paginate_links($pag_args);
?>
</div>
</div>
SOLVED:
Add this to get the category
get_header();
$catID = get_queried_object_id();
Pass that to the query:
$args = array(
[...]
'cat' => $catID
);
$wp_query = new WP_Query( $args );
The problem is that you are not using your $wp_query variable after its instanciation.
Your while loop should look like this:
while ( $wp_query->have_posts() ) : $wp_query->the_post();?>
/*Your inner code here*/
<?php endwhile;?>
You can create the category.php file if it' not exists. Then Inside your file
<?php
get_header();
$catID = get_queried_object_id();
?>
<?php
if ( have_posts() ) : ?>
<header class="page-header no-image">
<?php
the_archive_title( '<h1 class="page-title u-text-center">', '</h1>' );
?>
</header><!-- .page-header -->
<?php get_template_part( 'template-parts/filter-category' ); ?>
<?php
/*QUERY:
This is the problem if I move the query from there the category works but
If I leave the query the pagination doesn't have the post_per_page argument to take and doesn't work...
*/
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'post_type'=>'post',
'post_status'=>'publish',
'posts_per_page' => 6,
'cat' => $catID,
'paged' => $paged
);
$wp_query = new WP_Query( $args );
/*END QUERY*/
?>
<div id="blogpost-list" class="container">
<div class="row blogpost-wrapper">
<?php
/* Start the Loop */
$i = 1;
while ( $wp_query->have_posts() ) : $wp_query->the_post();?>
<?php if ($i % 3 == 0): ?>
<div class="blogpost-item-grid">
<!-- some stuff to display the post.. doesn't really matter-->
<?php get_template_part( 'template-parts/content-post-preview-large' ); ?>
</div>
<?php else: ?>
<div class="blogpost-item-grid">
<!-- some stuff to display the post doesn't really matter -->
<?php get_template_part( 'template-parts/content-post-preview' ); ?>
</div>
<?php endif; ?>
<?php $i++; ?>
<?php endwhile;?>
</div>
</div>
<?php get_template_part( 'template-parts/pagination' ); ?>
<?php
else :
get_template_part( 'template-parts/content', 'none' );
endif; ?>
<?php
get_footer();

Display Posts Category Wise

I have code to show the latest 3 posts of every category and I want to exclude the child categories, but it doesn't exclude the sub category and it shows sub category separately and it show the latest 3 posts of sub category. Is there any way to exclude sub categories from loop.
Here is the code:
<?php
//start page loop
if (have_posts()) : while (have_posts()) : the_post();
//get meta to set parent category
$library_filter_parent = '';
$library_parent = get_post_meta($post->ID, 'wts_library_parent', true);
if($library_parent != 'select_library_parent') { $library_filter_parent = $library_parent; } else { $library_filter_parent = NULL; }
?>
<div id="library-by-category-wrap">
<?php
//get meta to set parent category
$library_filter_parent = '';
$library_parent = get_post_meta($post->ID, 'wts_library_parent', true);
if($library_parent != 'select_library_parent') { $library_filter_parent = $library_parent; } else { $library_filter_parent = NULL; };
//term loop
$terms = get_terms('library_cats','orderby=custom_sort&hide_empty=1&child_of='.$library_filter_parent.'');
foreach($terms as $term) { ?>
<div class="heading">
<h2><?php echo $term->name; ?></h2>
</div>
<div class="library-category">
<?php
//tax query
$tax_query = array(
array(
'taxonomy' => 'library_cats',
'terms' => $term->slug,
'field' => 'slug'
)
);
$term_post_args = array(
'post_type' => 'library',
'numberposts' => '3',
'tax_query' => $tax_query
);
$term_posts = get_posts($term_post_args);
//start loop
foreach ($term_posts as $post) : setup_postdata($post);
//get images
$featured_image = get_the_post_thumbnail($post->ID, 'cat-thumbnail'); ?>
<?php if(!empty($featured_image)) { ?>
<div class="library-item">
<a class="library-title" href="#" title="<?php the_title(); ?>" target="_blank">
<h3><?php the_title(); ?></h3>
</a>
</div>
<!-- /library-item -->
<?php } ?>
<?php endforeach; ?>
</div>
<!-- /library-category -->
<?php } wp_reset_postdata(); ?>
</div>
<!-- /library-by-category-wrap -->
<?php wp_reset_query(); ?>
<?php endwhile; endif; ?>
<?php
//get all terms (e.g. categories or post tags), then display all posts in each term
$taxonomy = 'category';// e.g. post_tag, category
$param_type = 'category__in'; // e.g. tag__in, category__in
$term_args=array(
'orderby' => 'name',
'order' => 'ASC'
);
$terms = get_terms($taxonomy,$term_args);
if ($terms) {
foreach( $terms as $term ) {
$args=array(
"$param_type" => array($term->term_id),
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
echo 'List of Posts in '.$taxonomy .' '.$term->name;
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<p><?php the_title(); ?></p>
<?php
endwhile;
}
}
}
wp_reset_query(); // Restore global post data stomped by the_post().
?>
Note see the Time Parameters in the query_posts() article.
<?php // get all the categories from the database
$cats = get_categories();
// loop through the categries
foreach ($cats as $cat) {
// setup the cateogory ID
$cat_id= $cat->term_id;
// Make a header for the cateogry
echo "<h2>".$cat->name."</h2>";
// create a custom wordpress query
query_posts(“cat=$cat_id&post_per_page=100");
if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php // create our link now that the post is setup ?>
<?php the_title(); ?>
<?php echo '<hr/>'; ?>
<?php endwhile; endif;
// done our wordpress loop. Will start again for each category ?>
<?php } // done the foreach statement ?>
<?php
$catquery = new WP_Query( array( 'post_type' => 'testimonials', 'category_name'=>'hello','posts_per_page' => 10 ) );
while($catquery->have_posts()) : $catquery->the_post();
?>
<ul>
<li>
<h3>
<?php the_title(); ?>
</h3>
</li>
<li>
<?php the_content(); ?>
</li>
</ul>
<?php endwhile; ?>
$taxonomy = 'category';
$term_args = array(
'orderby' => 'name',
'order' => 'ASC',
'parent' => 0
);
$terms = get_terms($taxonomy, $term_args);
[homepage_cat_grid limit='3' cat_id='2']
add_shortcode( 'homepage_cat_grid', array($this, 'homepage_postgrid_shortcode') );
/**
* HOME PAGE CATEGORY WISE POST GRID
*/
public function homepage_postgrid_shortcode( $atts, $content ) {
extract( shortcode_atts( array (
'order' => '',
'orderby' => '',
'limit' => '',
'cat_id'=> ''
), $atts ) );
ob_start();
$args = array(
'post_type' => 'post',
'order' => $order,
'orderby' => $orderby,
'posts_per_page' => $limit,
'cat' => $cat_id
);
$home_post = new WP_Query( $args );
if( $home_post->have_posts() ) : ?>
<div class="post-header">
<div class="head-left">
<?Php
$terms = wp_get_post_terms($home_post->post->ID,'category');
$cat_nm=$terms[0]->name;
if($cat_nm=='News'): echo "Recent News"; else: echo $cat_nm; endif;
?>
</div>
<a href="<?php
$category_link = get_category_link( $terms[0]->term_id );
echo $category_link; ?>" class="btn post-head-btn">VIEW ALL</a>
</div>
<div class="row">
<?php while ( $home_post->have_posts() ) : $home_post->the_post(); ?>
<div class="col-md-4">
<div class="single-grid">
<a href="<?php echo get_permalink( $post->ID ); ?>">
<?php the_post_thumbnail( $post->ID ); ?>
</a>
<div class="post-caption">
<?php echo the_category(); ?>
<div class="post-heading">
<?php echo the_title(); ?>
</div>
<div class="post-content">
<?php the_excerpt(); ?>
</div>
<div class="row">
<div class="col-12">
<a class="btn btn-dark cust-btn" href="<?php echo get_permalink( $post->ID ); ?>">Read More</a>
</div>
</div>
</div>
</div>
</div>
<?php endwhile; wp_reset_query(); ?>
</div>
<?php else :
get_template_part( 'template-parts/content', 'none' );
endif;
return ob_get_clean();
}

Wordpress Category Template with Offset - Featured on every page

I'm trying to make a category template in which, first post is displayed with full text + big thumbnail (as featured) and then posts with titles only + a smaller thumbnail.
I've managed (with help) to do this with two queries, but first post remains the same on each page, won't change, as also, the loop won't adopt the offset of first page into other pages too.
Code is here:
<?php $cat_link = get_category_link( $cat_id );
$cat_name = get_cat_name($cat_id);
$cat_id = 22; ?>
<?php $latest_cat_post = new WP_Query( array('posts_per_page' => 1, 'category__in' => array($cat_id)));
if( $latest_cat_post->have_posts() ) : while( $latest_cat_post->have_posts() ) : $latest_cat_post->the_post(); ?>
<div class="catrecent">
<div class="recenttitle">
<h2 class="catidtxt"> <?php echo ($cat_name); ?></h2>
<h2 class="recentposttitle"><?php the_title(); ?></h3>
</div>
<?php if( has_post_thumbnail() ) { ?>
<div class="recentpostwrap">
<?php the_post_thumbnail( 'thumbcatbig' ); ?>
<?php the_content_limit(588,""); ?>
<div class="readrecent">
<?php _e("more", "mm"); ?>
</div>
</div>
<?php } else { ?>
<div class="holder no-thumb-big">
<?php the_content_limit(798,""); ?>
<div class="more">
<?php _e("more", "mm"); ?>
</div>
</div>
<?php } ?>
<?php endwhile; else: ?><?php endif; ?><?php wp_reset_query(); ?>
</div>
<ul class="catlist-recent">
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$ppp = 7;
$offset = 1;
$cat_id = '22,27,29';
//Manually determine page query offset (offset + current page (minus one) x posts per page)
$page_offset = $offset + ( ($paged - 1) * $ppp );
$query = new wp_query(array(
'offset' => $page_offset,
'posts_per_page' => $ppp,
'cat' => $cat_id,
'paged' => $paged
));
if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post(); ?>
<li class="catlist-recentpost">
<?php the_post_thumbnail( 'thumbcatsmall' ); ?>
<?php the_title(); ?>
<div class="catlist-recentposttxt"> <?php the_content_limit(300, ''); ?></div>
</li>
<?php endwhile; else: ?><?php endif; ?>
</ul>
<div class="cat-pagenate">
<?php
// pagination
$big = 999999999; // need an unlikely integer
echo paginate_links(array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'total' => ceil(($query->found_posts - $offset) / $ppp),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
));
wp_reset_query();
?>
</div>
Any help appreciated!
Try
$query = new wp_query(array(
'offset' => $page_offset,
'number' => $ppp,
'cat' => $cat_id,
'page' => $paged
));
Note the change from "paged" to "page"

How to push a page's custom field data as a value within a template file?

Long story short. I'm trying to expand the functionality of a portfolio template. It currently can only handle one page. I can duplicate the template, relying on custom fields to pull in specific portfilio items (the theme uses custom post types).
So, here's the bit from my portfolio-template.php:
$args = array(
'post_type' => 'portfolio',
'orderby' => 'menu_order',
'order' => 'ASC',
'meta_value' => 'ao',
'posts_per_page' => -1
);
So, I want to create a new page, select the portfolio template, then define a custom field on that page that will pass on the meta_value to the portfolio-template.php
So it would look like this instead:
$args = array(
'post_type' => 'portfolio',
'orderby' => 'menu_order',
'order' => 'ASC',
'meta_value' => 'PULL_VALUE_FROM_CUSTOM_FIELD',
'posts_per_page' => -1
);
With "PULL_VALUE_FROM_CUSTOM_FIELD" being the bit that I can't figure out. Sorry if my vernacular is code_crude, I'm new at this!
EDIT: Adding in the full code of the template
<?php
$args = array(
'post_type' => 'portfolio',
'orderby' => 'menu_order',
'order' => 'ASC',
'meta_value' => 'ao',
'posts_per_page' => -1
);
$portfolio_query = new WP_Query($args);
if( $portfolio_query->have_posts() ) :
echo '<div id="primary" class="hfeed">';
while( $portfolio_query->have_posts() ) : $portfolio_query->the_post();
// styles
$style = 'zilla-' . get_post_meta($post->ID, '_zilla_portfolio_style', true);
$media_pos = 'zilla-' . get_post_meta($post->ID, '_zilla_portfolio_media_position', true);
$width = ( $media_pos == 'zilla-media-center' ) ? 940 : 600;
// project url
$portfolio_url = get_post_meta($post->ID, '_zilla_portfolio_project_url', true);
if( !empty($portfolio_url) )
$portfolio_button_copy = get_post_meta($post->ID, '_zilla_portfolio_project_url_copy', true);
// determine which media to display
$portfolio_display_gallery = get_post_meta($post->ID, '_zilla_portfolio_display_gallery', true);
$portfolio_display_video = get_post_meta($post->ID, '_zilla_portfolio_display_video', true);
$portfolio_display_audio = get_post_meta($post->ID, '_zilla_portfolio_display_audio', true);
// grab everything else
$custom_bg = get_post_meta($post->ID, '_zilla_portfolio_display_background', true);
$portfolio_caption = get_post_meta($post->ID, '_zilla_portfolio_caption', true);
?>
<?php zilla_page_before(); ?>
<!--BEGIN .hentry-->
<div <?php post_class( $style . ' ' . $media_pos ) ?> id="post-<?php the_ID(); ?>">
<?php zilla_page_start(); ?>
<div class="hentry-inner">
<!--BEGIN .entry-media -->
<div class="entry-media">
<?php
if( $portfolio_display_gallery == 'on' ) {
$gallery_layout = get_post_meta( $post->ID, '_zilla_gallery_layout', true);
$slideshow = ( $gallery_layout == 'slideshow' ) ? true : false;
$size = ( $media_pos == 'zilla-media-center' ) ? 'portfolio-full' : 'portfolio-index';
zilla_gallery( $post->ID, $size, $slideshow, $slideshow );
}
if( $portfolio_display_video == 'on' ) {
$embed = get_post_meta($post->ID, '_zilla_video_embed_code', true);
if( !empty( $embed ) ) {
echo stripslashes(htmlspecialchars_decode($embed));
} else {
zilla_video( $post->ID, $width );
}
}
if( $portfolio_display_audio == 'on' ) {
zilla_audio( $post->ID, $width );
}
?>
<!--END .entry-media -->
</div>
<!--BEGIN .entry-content -->
<div class="entry-content">
<h2 class="entry-title"><?php the_title(); ?></h2>
<?php if( !empty($portfolio_caption) )
echo "<p class='zilla-caption'>" . stripslashes(htmlspecialchars_decode($portfolio_caption)) . "</p>"; ?>
<?php if( !empty($portfolio_url) && $media_pos == 'zilla-media-center' ) { ?>
<?php echo $portfolio_button_copy; ?>
<?php } ?>
<?php the_content(); ?>
<?php wp_link_pages(array('before' => '<p><strong>'.__('Pages:', 'zilla').'</strong> ', 'after' => '</p>', 'next_or_number' => 'number')); ?>
<?php if( !empty($portfolio_url) && ( $media_pos == 'zilla-media-left' || $media_pos == 'zilla-media-right' ) ) { ?>
<?php echo $portfolio_button_copy; ?>
<?php } ?>
<!--END .entry-content -->
</div>
</div>
<?php zilla_page_end(); ?>
<!--END .hentry-->
</div>
<?php zilla_page_after(); ?>
<?php endwhile; ?>
<!--END #primary .hfeed-->
</div>
<?php else: ?>
<div id="content">
<!--BEGIN #post-0-->
<div id="post-0" <?php post_class(); ?>>
<h2 class="entry-title"><?php _e('Error: No Portfolios Found', 'zilla') ?></h2>
<!--BEGIN .entry-content-->
<div class="entry-content">
<p><?php _e("Sorry, but no portfolios have been created.", "zilla") ?></p>
<!--END .entry-content-->
</div>
<!--END #post-0-->
</div>
</div>
<?php endif; ?>
Before the Loop, you can access the global variable $post, which in case of single.php and page.php will represent that very post type (post/page/cpt) object that will be displayed by the template file.
If not mistaken, when used in category.php, for example, it will grab the first one.
So, in the case in question:
<?php
global $post;
$the_meta = get_post_meta( 'custom_field_name', $post->ID );
if( !$the_meta )
$the_meta = 'your_backup_solution';
$args = array(
'post_type' => 'portfolio',
'orderby' => 'menu_order',
'order' => 'ASC',
'meta_value' => $the_meta,
'posts_per_page' => -1
);
$portfolio_query = new WP_Query($args);
// et cetera

Resources