Display post for a week - wordpress

Can someone help me how to display the post only for a week? i have this code that works fine in post_date:
<?php
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'cat' => 1,
'orderby' => 'date',
'order' => 'DESC',
// Using the date_query to filter posts from last week
'date_query' => array(
array(
'after' => '1 week ago'
)
)
);
?>
<ul class="weekly-list">
<?php $the_query = new WP_Query( $args ); ?>
<?php while ( $the_query->have_posts() ) { $the_query->the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</li>
<?php } wp_reset_postdata(); ?>
</ul>
but how to do it in the custom field date. Because my other events is posted a week before the events. Can someone help me?
Thanks.

This Might help you to get solution:
$week = date('W');
$year = date('Y');
$the_query = new WP_Query( 'year=' . $year . '&w=' . $week );
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>

The previous answer was along the good track. Just modify your date_query:
<?php
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'cat' => 1,
'orderby' => 'date',
'order' => 'DESC',
// Using the date_query to filter posts from last week
'date_query' => array(
array(
'year' => date( 'Y' ),
'week' => date( 'W' ),
),
),
);
?>
<ul class="weekly-list">
<?php $the_query = new WP_Query( $args ); ?>
<?php while ( $the_query->have_posts() ) { $the_query->the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</li>
<?php } wp_reset_postdata(); ?>
</ul>

Related

Alternatives to using get_pages in WordPress to get children page data

I am using get_pages to fetch some data from the children pages of a parent in WordPress (like a custom loop) - but it doesnt work when trying to fetch some data as set by the Advanced Custom Fields plugin for some strange reason... Is there an alternative / better way to acheive what I want? Code below works apart from fetching the ACF field called 'job_title'.
<?php
$args = array(
'parent' => $post->ID,
'post_type' => 'page',
'numberposts' => -1,
'post_status' => 'publish',
'sort_order' => 'DESC',
'sort_column" => "post_name',
'orderby' => 'menu_order',
'order' => 'ASC'
);
$pages = get_pages($args); ?>
<div class="childrenFetchedLoopWrapper">
<?php foreach( $pages as $page ) { ?>
<div class="feedItemWrapper wpb_animate_when_almost_visible wpb_fadeInUp fadeInUp" style="background-image: url('<?php echo get_the_post_thumbnail_url($page->ID, 'full'); ?>')">
<a href="<?php echo get_permalink($page->ID); ?>" rel="bookmark" title="<?php echo $page->post_title; ?>">
<img src="/wp-content/themes/salient-child/images/aspectTrans.png" alt="*" title="*" />
<h3><?php echo $page->post_title; ?></h3>
<p><?php the_field('job_title'); ?></p>
</a>
</div>
<?php } ?>
</div>
Replace <?php the_field('job_title'); ?> with <?php the_field('job_title', $page->ID); ?>.
OR
You can use WP_Query for alternative solution.
or you can also get acf value using get_post_meta();
Try Out this code. I hope it helps.
<?php
$args = array(
'post_type' => 'page',
'posts_per_page' => -1,
'post_parent' => $post->ID,
'order' => 'ASC',
'orderby' => 'menu_order'
);
$parent = new WP_Query( $args );
if ( $parent->have_posts() ) : ?>
<?php while ( $parent->have_posts() ) : $parent->the_post();
$id = get_the_ID(); ?>
<p><?php the_field('job_title', $id); ?></p>
<?php endwhile; ?>
<?php endif; wp_reset_postdata(); ?>

How to add taxonomies as an argument in wp_query

This a part of a program to display a product list. I created the products as custom posts, and I need to display the posts on the basis of taxonomies.
Who can I provide taxonomies as an argument in post selection arguments?
I created taxonomies and posts using tool set plugin.
<?php $taxonomies = get_terms([
'taxonomy' => 'product_category',
'parent' => 0,
'orderby' => 'name',
'order' => get_query_var('Order', 'ASC'),
'hide_empty' => false,
]);
?><?php
$i=1;
if ($taxonomies) {
foreach ($taxonomies as $taxonomy ) {; ?>
<?php
if(!empty(do_shortcode('[types term_id="'.$taxonomy->term_id.'" termmeta="secondary-category-image" size="product-main-cat" url="true"][/types]'))){
$pro_cat_img = do_shortcode('[types term_id="'.$taxonomy->term_id.'" termmeta="secondary-category-image" size="product-main-cat" url="true"][/types]');
}else{
$pro_cat_img = get_template_directory_uri()."/img/product-guitar.png";
} ?>
<h1>
<?php echo $taxonomy->name;
//$ir=$taxonomy->name;
?>
</h1>
<ul>
<?php
// args
$args = array(
'posts_per_page' => 5,
'post_type' => 'product',
'post_status' => 'publish',
//how can i add taxonomies asan argument here
);
// query
$the_query = new WP_Query( $args );
$count = $the_query->post_count;
if( $the_query->have_posts() ):
while( $the_query->have_posts() ) : $the_query->the_post();
if(has_post_thumbnail(get_the_ID())) {
$feature_image_full = simplexml_load_string(get_the_post_thumbnail(get_the_ID(), 'banner_img'));
$pr_img_source = $feature_image_full->attributes()->src;
?>
<li>
<span>
<?php echo the_title(); ?>
</span>
</li>
<?php } endwhile; endif; wp_reset_query(); ?>
</ul>
Here is the working code
<?php $taxonomies = get_terms([
'taxonomy' => 'product_category',
'parent' => 0,
'orderby' => 'name',
'order' => get_query_var('Order', 'ASC'),
'hide_empty' => false,
]);
?><?php
$i=1;
if ($taxonomies) {
foreach ($taxonomies as $taxonomy ) {; ?>
<?php
if(!empty(do_shortcode('[types term_id="'.$taxonomy->term_id.'" termmeta="secondary-category-image" size="product-main-cat" url="true"][/types]'))){
$pro_cat_img = do_shortcode('[types term_id="'.$taxonomy->term_id.'" termmeta="secondary-category-image" size="product-main-cat" url="true"][/types]');
}else{
$pro_cat_img = get_template_directory_uri()."/img/product-guitar.png";
} ?>
<div class="product-row clearfix">
<div class="bg-box">
<?php if($i==1){?>
<img src="<?php echo esc_url($pro_cat_img); ?>" class="product-guitar"/>
<?php }else if($i==2){ ?>
<img src="<?php echo esc_url($pro_cat_img); ?>" class="product-speaker"/>
<?php }else if($i==3){ ?>
<img src="<?php echo esc_url($pro_cat_img); ?>" class="product-headphone"/>
<?php } ?>
<a href="<?php echo esc_url( add_query_arg( array('cat'=>$taxonomy->term_id), home_url('/products') ) ); ?>" class="view-more-btn button button--moema ">
READ MORE
</a>
</div>
<div class="items-list-box">
<h1>
<?php echo $taxonomy->name;
$ir=$taxonomy->name;
?>
</h1>
<ul>
<?php
// args
$args = array(
'posts_per_page' => 5,
'post_type' => 'product',
'post_status' => 'publish',
'tax_query' => array(
array (
'taxonomy' => 'product_category',
'field' => 'name',
'terms' => $ir,
)
),
);
// query
$the_query = new WP_Query( $args );
$count = $the_query->post_count;
if( $the_query->have_posts() ):
while( $the_query->have_posts() ) : $the_query->the_post();
if(has_post_thumbnail(get_the_ID())) {
$feature_image_full = simplexml_load_string(get_the_post_thumbnail(get_the_ID(), 'banner_img'));
$pr_img_source = $feature_image_full->attributes()->src;
?>
<li>
<span>
<?php echo the_title(); ?>
</span>
</li>
<!-- <img src="<?php //echo esc_url($pr_img_source); ?>" alt="<?php the_title(); ?>">
-->
<?php } endwhile; endif; wp_reset_query(); ?>
</ul>

Adding pagination to contents

So, I have following code to display products in Woocommerce
<?php
get_header()
?>
<div class="rfp_hide" id="rhm_product_show">
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'product',
'paged' => $paged,
'posts_per_page' => 20,
'product_cat' => '',
'orderby' => 'date',
'order' => 'DESC'
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
global $product, $userpro, $post, $paged;
?>
<div class="rhm_indi_product rhm_profile_items">Contents</div>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
<nav>
<ul>
<li><?php previous_posts_link( '« PREV', $loop->max_num_pages) ?></li>
<li><?php next_posts_link( 'NEXT »', $loop->max_num_pages) ?></li>
</ul>
</nav>
</div>
I am getting /page/2 but the content does not change and only displays the same stuffs.
Am I doing it right?
Thanks

Pagination for my custom post type in widget

I have displayed the custom post type in the widget. Now i want to add pagination in the last. because i have more than 10 posts in my custom post type.
<ul class="posts-list">
<?php if (have_posts()) : ?>
<?php
global $post;
$cats = get_the_category();
$cat_name = $cats[0]->name;
$args = array(
'posts_per_page' => 10,
'offset' => 0,
'category' => $cat_name,
'orderby' => 'post_date',
'order' => 'DESC',
'post_status' => 'publish',
'suppress_filters' => true );
$previous_post = get_posts($args);
foreach ( $previous_post as $post ) :
setup_postdata( $post ); ?>
<li>
<h5><?php the_title(); ?></h5>
<p>posted on <?php the_time(' F jS, Y') ?> by <?php the_author(); ?></p>
<p>Posted in <?php echo $cat_name->name; $cat_name = get_the_category($post->ID); ?></p>
</li>
<?php endforeach;
wp_reset_postdata(); ?>
<?php endif; ?>
</ul>
Try this one and enter your custom post type's name in 'post_type' => 'your custom post type name'
<ul class="posts-list">
<?php if (have_posts()) : ?>
<?php
global $post;
$paged1 = isset( $_GET['paged1'] ) ? (int) $_GET['paged1'] : 1;
$cats = get_the_category();
$cat_name = $cats[0]->name;
$args = array(
'posts_per_page' => 10,
'offset' => 0,
'category' => $cat_name,
'orderby' => 'post_date',
'paged' => $paged1,
'post_type' => 'your custom post type name'
'order' => 'DESC',
'post_status' => 'publish',
'suppress_filters' => true );
$previous_post = get_posts($args);
foreach ( $previous_post as $post ) :
setup_postdata( $post ); ?>
<li>
<h5><?php the_title(); ?></h5>
<p>posted on <?php the_time(' F jS, Y') ?> by <?php the_author(); ?></p>
<p>Posted in <?php echo $cat_name->name; $cat_name = get_the_category($post->ID); ?></p>
</li>
<?php endforeach;
?>
<?php endif;
$pag_args1 = array(
'format' => '?paged1=%#%',
'current' => $paged1,
'total' => $previous_post->max_num_pages,
'add_args' => array( 'paged1' => $paged1 )
);
echo paginate_links( $pag_args1 );
wp_reset_postdata(); ?>
</ul>

Display two posts from each post type with wp_query

I have used the following code to display the one post from each post.
$post_types = array('a', 's','d','f','g');//post type names
foreach( $post_types as $post_type) :
// The Query
$the_query = new WP_Query( array( 'post_type' => $post_type,
'orderby' => 'post_date',
'order' => 'DESC',
));
// The Loop
?>
<?php
while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php if ( has_post_thumbnail() ) : ?>
<div class="issue-content masonItem <?php echo $post_type; ?>">
<?php
$url = wp_get_attachment_url( get_post_thumbnail_id() );
$resizedUrl = get_bloginfo('template_url')."/libs/timthumb.php?src=".$url."&w=327&h=204&zc=1"; ?>
<img src="<?php echo $resizedUrl; ?>" alt="<?php the_title(); ?>" class="masonry-thumb" />
<?php get_template_part( 'content-issues', 'page' );?>
</div><!--issue-content--><!--Mason Item-->
<?php endif; ?>
<?php endwhile;
Now i want to display 2 posts from each post type. How can i do this ? I got the $post_count idea. But i cant know use it in my code.
Try this :
'posts_per_page' => 2,
You try This
$the_query = new WP_Query( array( 'post_type' => $post_type,'orderby' => 'post_date','posts_per_page' => 2,'order' => 'DESC'));

Resources