I'm going nuts trying to figure out why I'm having such a difficult time getting WordPress to only show posts newer than 30-days and I could really use a second set of eyes. I'm using the following code but nothing is showing up on my site.
<?php
// Create a new filtering function that will add our where clause to the query
function filter_where( $where = '' ) {
// posts in the last 30 days
$where .= " AND post_date > '" . date('Y-m-d', strtotime('-30 days')) . "'";
return $where;
}
add_filter( 'posts_where', 'filter_where' );
$the_query = new WP_Query( $query_string );
remove_filter( 'posts_where', 'filter_where' );
?>
<?php if ($the_query->have_posts()) : ?>
<?php while ($the_query->have_posts()) : $the_query->the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<h2 class="title"><?php the_title(); ?></h2>
<div class="meta">
<span class="date">
<strong><?php the_time('d'); ?></strong>
<strong><?php the_time('M'); ?></strong>
</span>
</div>
<div class="entry">
<?php if ( function_exists( 'get_the_image' ) ) {
get_the_image( array( 'custom_key' => array( 'post_thumbnail' ), 'default_size' => 'full', 'image_class' => 'alignleft', 'width' => '170', 'height' => '155' ) ); }
?>
<?php the_content('Read More'); ?>
</div>
</div>
<?php endwhile; ?>
<div class="navigation">
<?php
include('includes/wp-pagenavi.php');
if(function_exists('wp_pagenavi')) { wp_pagenavi(); }
?>
</div>
<?php else : ?>
<h2 class="center">Not Found</h2>
<p class="center">Sorry, but you are looking for something that isn't here.</p>
<?php get_search_form(); ?>
<?php endif; ?>
<?php
function filter_where($where = '') {
//posts in the last 30 days
$where .= " AND post_date > '" . date('Y-m-d', strtotime('-30 days')) . "'";
return $where;
}
add_filter('posts_where', 'filter_where');
query_posts($query_string);
?>
This works
The way you're doing looks fine. Try putting posts_per_page => -1 as the argument.
The following works for me:
function filter_where( $where = '' ) {
$where .= " AND post_date > '" . date('Y-m-d', strtotime('-30 days')) . "'";
return $where;
}
add_filter( 'posts_where', 'filter_where' );
$args = array(
'posts_per_page' => -1,
);
$the_query = new WP_Query($args);
remove_filter( 'posts_where', 'filter_where' );
while ($the_query->have_posts()) {
$the_query->the_post();
// do stuff
}
Hope it helps.
I know this is super old but, you can do this with WP_Query now.
$args = array(
'post_type' => 'post', // or whatever post type you want
'posts_per_page' => -1, // get all posts that apply, i.e. no limit
'date_query' => array(
array(
'after' => '-30 days',
'column' => 'post_date',
),
),
);
Related
function diwp_services_custom_post_type()
{
$labels = array(
'name' => 'Services',
'singular_name' => 'Service',
'add_new' => 'Add Service',
'add_new_item' => 'Enter Service Details',
'all_items' => 'All Services',
'featured_image' => 'Add Service Image',
'set_featured_image' => 'Set Service Image',
'remove_featured_image' => 'Remove Service Image'
);
// Set Options for this custom post type;
$args = array(
'public' => true,
'label' => 'Services',
'labels' => $labels,
'description' => 'Services is a collection of services and their info',
'menu_icon' => 'dashicons-hammer',
'supports' => array('title', 'editor', 'thumbnail'),
'capability_type' => 'page',
);
register_post_type('services', $args);
}
add_action('init', 'diwp_services_custom_post_type');
// >> Create Shortcode to Display Services Post Types
function diwp_create_shortcode_services_post_type()
{
$args = array(
'post_type' => 'services',
'posts_per_page' => '10',
'publish_status' => 'published',
);
$query = new WP_Query($args);
if ($query->have_posts()) :
while ($query->have_posts()) :
$query->the_post();
$result .= '<div class="card">';
$result .= '<div class="card-block block-1">';
$result .= ' <h3 class="card-title">' . get_the_title() . '</div>';
$result .= ' <p class="card-text">' . get_the_content() . '</div>';
$result .= '</div>';
$result .= '</div>';
endwhile;
wp_reset_postdata();
endif;
return $result;
}
add_shortcode('services-list', 'diwp_create_shortcode_services_post_type');
// shortcode code ends here
I visited below link and implemented same as that but find no results
DIVEIN WP
Here is the screenshot as I am working on localhost
Any help regarding this will be appreciatable.
Thanks
First, you must fetch permalink from Setting >> permalink, and click Save changes
Seconds, Now you must add do_shortcode on the position you need to show all post from custom post type like :
echo do_shortcode('[services-list]');
I test your code in my end and work good https://abukotsh.me/wp/services/test-service/
Scroll down and you see test services
So I'm trying to get pagination correct for my wordpress site so categories and pages display the correct posts :
wp_reset_query();
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$sticky = get_option( 'sticky_posts' );
$args = array(
'ignore_sticky_posts' => 1,
'posts_per_page' => 10,
'orderby' => 'date',
'post__not_in' => $sticky,
'paged' => $paged,
);
$query = new WP_Query( $args );
?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
I get the posts to display but the following parameters :
'ignore_sticky_posts' => 1,
'posts_per_page' => 10,
'orderby' => 'date',
'post__not_in' => $sticky,
does not seem to work... any idea why ?
Chances are you don't need wp_reset_query() at the top unless you are overriding the global $wp_query somewhere else in your code.
When you are creating a custom query you need to use that throughout your loop. In your case that is $query so we need to use that when calling have_posts(), the_post(), and max_num_pages
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$sticky = get_option( 'sticky_posts' );
$args = array(
'ignore_sticky_posts' => 1,
'posts_per_page' => 10,
'orderby' => 'date',
'post__not_in' => $sticky,
'paged' => $paged,
);
$query = new WP_Query( $args );
?>
<?php if ( $query->have_posts() ) : ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
// interact with the post here
<?php endwhile; ?>
<?php endif; ?>
// we overwrote the global $post when called the_post(); so we need to reset that
<?php wp_reset_postdata(); ?>
// again we need to reference our custom query for our pagination
<?php
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $query->max_num_pages
) );
?>
I'm trying to get this code to pull only the "stills" category on my Wordpress page. I tried a few different things but it just pulls all of the category thumbnails.
<?php
$args = array( 'numberposts' => '12' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
if ( has_post_thumbnail($recent["ID"])) {
echo '<li>' . get_the_post_thumbnail($recent["ID"], 'thumbnail') . '</li>';
}
}?>
You can try:
<?php
$args = array( 'numberposts' => '12' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
if ( has_post_thumbnail() ) {
if (is_category('stills')){
the_post_thumbnail( 'thumbnail' );
}
}
}?>
i want captcha code security in my signup page but when i fill the form with wrong captcha code and submit form. problem is view two page in my signup page. and fill this form again with correct captcha but they same problem again and show incorrect captcha.
CONTROLLER
public function user_signup($data = NULL){
$this->load->library('form_validation');
$this->recaptcha->recaptcha_check_answer($_SERVER['REMOTE_ADDR'],$this->input->post('recaptcha_challenge_field'),$this->input->post('recaptcha_response_field'));
$this->form_validation->set_rules('Username', 'Username', 'required|trim|is_unique[users.Username]');
$this->form_validation->set_rules('First_name', 'First Name', 'required|trim');
$this->form_validation->set_rules('Last_name', 'Last Name', 'required|trim');
$this->form_validation->set_rules('Email', 'Email', 'required|trim|valid_email');
$this->form_validation->set_rules('Password', 'Password', 'required|trim|md5');
$this->form_validation->set_rules('Conf_password', 'Confirm Password', 'required|trim|matches[Password]');
$this->form_validation->set_message('is_unique', 'Username already exists.');
if($this->form_validation->run() && $this->recaptcha->getIsValid()){
//Call to recaptcha to get the data validation set within the class.
$form_data = array();
$form_data['Username'] = $this->input->post('Username');
$form_data['First_name'] = $this->input->post('First_name');
$form_data['Last_name'] = $this->input->post('Last_name');
$form_data['Email'] = $this->input->post('Email');
$form_data['Password'] = $this->input->post('Password');
$form_data['Conf_password'] = $this->input->post('Conf_password');
$this->load->model('User');
$this->User->sign_up($form_data);
$data['msg']= "Account Created Sucessfuly";
}else{
if(!$this->recaptcha->getIsValid()){
$this->session->set_flashdata('error','incorrect captcha');
}
}
redirect("Users/signup", $data);
}
MODEL
public function sign_up($form_data){
$this->db->insert('users', $form_data);
}
VIEW
<?php
//$val = NULL;
if(isset($results->id)){
$val = "Update";
$action = "Users/user_update/".$results->id;
}else{
$val= "Register";
$action = "Users/user_signup";
}; ?>
<?php echo form_open($action); ?>
<?php echo form_input(array('name'=>'Username', 'class'=>'input-xlarge', 'type'=>'text', 'placeholder'=>'Username', 'required' => 'required', 'value'=>$u)); ?>
<?php echo form_error('Username', '<div class="alert alert-error">', '</div>'); ?>
<?php echo form_input(array('name'=>'First_name', 'class'=>'input-xlarge', 'type'=>'text', 'placeholder'=>'First Name', 'required' => 'required', 'value'=>$f)); ?>
<?php echo form_error('First_name', '<div class="alert alert-error">', '</div>'); ?>
<?php echo form_input(array('name'=>'Last_name', 'class'=>'input-xlarge', 'type'=>'text', 'placeholder'=>'Last Name', 'required' => 'required', 'value'=>$l)); ?>
<?php echo form_error('Last_name', '<div class="alert alert-error">', '</div>'); ?>
<?php echo form_input(array('name'=>'Email', 'class'=>'input-xlarge', 'type'=>'text', 'placeholder'=>'Email', 'required' => 'required', 'value'=>$e)); ?>
<?php echo form_error('Email', '<div class="alert alert-error">', '</div>'); ?>
<?php echo form_input(array('name'=>'Password', 'class'=>'input-xlarge', 'type'=>'password', 'placeholder'=>'Password')); ?>
<?php echo form_error('Password', '<div class="alert alert-error">', '</div>'); ?>
<?php echo form_input(array('name'=>'Conf_password', 'class'=>'input-xlarge', 'type'=>'password', 'placeholder'=>'Confirm Password')); ?>
<?php echo form_error('Conf_password', '<div class="alert alert-error">', '</div>'); ?>
<?php if(!isset($results->id)){echo $recaptcha_html;} ?>
<?php if ($this->session->flashdata('error') !== FALSE) { echo '<div class="alert alert-error">'.$this->session->flashdata('error').'</div>'; } ?>
<div class="login-actions">
<span><input type="checkbox"> <span class="remember">I have read & agree</span></span>
<span><?php echo form_submit(array('value'=>$val, 'class'=>'btn btn-large btn-warning pull-right', 'type'=>'submit')); ?></span>
</div>
<?php echo form_close(); ?>
Put this code after the if(!$this->recaptcha->getIsValid()){ and echo messages...
if($this->input->post('recaptcha_response_field') =="" ){
$this->session->set_flashdata('error','fill up this code');
}else{
$this->session->set_flashdata('error','incorrect captcha');
}
if your show the message "account create successfully". use the set flash message
read this link properly
http://ellislab.com/codeigniter/user-guide/libraries/sessions.html
How to display error messages in CodeIgniter
First you create a signup function in Controller and and view default signup page. as like
$this->load->view('User/User_sign_up');
In wordpress, the following function will echo out a list of categories with the posts associated with each category underneath each category name.
This works fine, except for the fact that this produces a flat structure. Some of the categories are child categories other categories, and I'd like to be able to output a list with a structure that matches this (kind of like a site map)
Is anyone able to help me figure out how to modify this code to achieve this?
function posts_by_category() {
//get all categories then display all posts in each term
$taxonomy = 'category';
$param_type = '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() ) { ?>
<div class="category section">
<h3><?php echo ''.$term->name;?></h3>
<ul>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<li><?php the_title(); ?></li>
<?php endwhile; ?>
</ul>
</div>
<?php
}
}
}
wp_reset_query(); // Restore global post data stomped by the_post().
}
Here is a code snippet I found the other day Hierarchical Category List with Post Titles, should do the trick.
<?php
/*****************************************************************
*
* alchymyth 2011
* a hierarchical list of all categories, with linked post titles
*
******************************************************************/
// http://codex.wordpress.org/Function_Reference/get_categories
foreach( get_categories('hide_empty=0') as $cat ) :
if( !$cat->parent ) {
echo '<ul><li><strong>' . $cat->name . '</strong></li>';
process_cat_tree( $cat->term_id );
}
endforeach;
wp_reset_query(); //to reset all trouble done to the original query
//
function process_cat_tree( $cat ) {
$args = array('category__in' => array( $cat ), 'numberposts' => -1);
$cat_posts = get_posts( $args );
if( $cat_posts ) :
foreach( $cat_posts as $post ) :
echo '<li>';
echo '' . $post->post_title . '';
echo '</li>';
endforeach;
endif;
$next = get_categories('hide_empty=0&parent=' . $cat);
if( $next ) :
foreach( $next as $cat ) :
echo '<ul><li><strong>' . $cat->name . '</strong></li>';
process_cat_tree( $cat->term_id );
endforeach;
endif;
echo '</ul>';
}
?>