Introduction
I have a blog where I am showing latest post within the last 365 days and only show 100 post. I also have the option for the user to select year (this is what im currently working on).
Question
How can I have it so when A user chooses a different year (from the selection) the query is set to show post from that year and remove the 100 post limit and show this is my blog page.
My Query
<?php
// gets info
$current_category = get_queried_object('post');
//$current_user =get_the_author_posts();
// the query
$wpb_all_query = new WP_Query(array(
'post_type' => 'post',
'post_status' => 'publish',
'cat' => $current_category->term_id,
// 'author' => $current_user,
// 'tag' => '',
'posts_per_page' => 100,
'date_query' => array(
array(
// 'year' => '2019',
'after' => '-365 days',
'column' => 'post_date',
)
)
));
My Selection (year)
<select style="top: -4px;"
class=" btn-link col-12"
name="archive-dropdown"
onChange='document.location.href=this.options[this.selectedIndex].value;'>
<?php wp_get_archives('type=yearly&format=option'); ?>
</select>
My Blog Page
<?php
require "settings.php";
if ($wpb_all_query->have_posts())
:while ($wpb_all_query->have_posts())
:$wpb_all_query->the_post();
?>
content
<?php endwhile; ?>
<?php endif; ?>
I was trying a statement in the query but could not see how to get year selected , but then if i set the year to a var depending on the option maybe that's better>?
This will give you all posts (no limit to 100), for the year 1999. Change $yearToLookFor to the year you need.
<?php
//here is from a $_GET parameter
$yearToLookFor = $_GET['year'];
// gets info
$current_category = get_queried_object('post');
//$current_user =get_the_author_posts();
// the query
$wpb_all_query = new WP_Query(array(
'post_type' => 'post',
'post_status' => 'publish',
'cat' => $current_category->term_id,
// 'author' => $current_user,
// 'tag' => '',
'date_query' => array(
array(
'year' => $yearToLookFor,
)
)
));
In this example, the year got taken from a url query, like this: https://yourwebiste.zyx/?year=1999
Related
I'm trying to show one post from several categories. My code just shows the first category post :\ any advice?
<?php
$args = array(
'cat' => 1,15,
'post_type' => 'post',
'posts_per_page' => '1',
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) :
while ($query->the_post()):
the_title();
the_post_thumbnail(array(200, 200));
?>
<?php endwhile;
endif;?>
Please follow the code to understand how to show post items from selected category items by passing the category ID.
$args = array(
'post_type' => 'post', // post type
'posts_per_page' => -1, // number of post items
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => array( 16, 244 ) // pass the ID of the post category, separated by a comma.
)
)
);
You defined 'posts_per_page' => '1' so you are getting exactly what you ask: 1 post. Either from category 1 or 15, whichever is the most recent post.
If you want 1 post from EACH category, I would just loop your code, with a different category each time (just 1).
Only thing is, that will be in order of the category IDs you give and not sorted on date on something else. Also, if you have a post in multiple categories, you might end up with the same post twice.
I need to have 5 random posts on post page (http://7cuteoutfits.com/2015/07/08/suits-rachel-z-office-fashion/) excluding current post. Random posts should be on chosen dates (for example posts from last 2 months until yesterday )
I added a few lines of code to single.php of my wordpress and now have 5 random posts. So I need to modify the code so that it will meet my requirements (above). I think it's 2 more lines, I'll be very thankful if you help.
<ul>
<?php
$currentID = get_the_ID();
$args = array( 'posts_per_page' => 5, 'orderby' => 'rand' );
$rand_posts = get_posts( $args);
foreach ( $rand_posts as $post ) :
setup_postdata( $post ); ?>
<li><?php the_title(); ?></li>
<?php endforeach;
wp_reset_postdata(); ?>
</ul>
You can use WP_Query for that.
global $post;
$args = array(
'post__not_in' => array($post->ID)
'orderby' => 'rand'
'date_query' => array(
array(
'after' => 'January 1st, 2015',
'before' => array(
'year' => 2015,
'month' => 07,
'day' => 9,
),
'inclusive' => true,
),
),
'posts_per_page' => 5,
);
$query = new WP_Query( $args );
This query orders randomly posts between today (inclusive) and Jan. 1st 2015
I haven't tested the snippet here, so please let me know if it does not work for you.
More info on WP_query and its usage (also for date parameters) here
Once you query with WP_Query, you have to
wp_reset_postdata();
just as you are already doing.
EDIT:
To show the post content, you can call
the_content()
to print it directly, or
get_the_content()
to get it as a return value. Then you can handle the printing later with the HTML markup you desire.
With use of the Advanced Custom Fields plugin I created a select dropdown which contains 6 membership types. All of my 'listings' using this custom field are assigned one of the 6.
I'd like to display all 'listings' by:
Ultimate Plus
Ultimate
Professional
Commercial
Business
Free
In this particular order, so those paying for the highest level membership have their 'listing' appear at the top of the page.
I expected it to be similar to this which I just found but unsure exactly:
// args
$args = array(
'post_type' => 'directory_listings',
'meta_key' => 'free',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => '#',
'value' => array( #, # ),
'compare' => 'IN',
),
),
);
// query
$wp_query = new WP_Query( $args );
?>
<?php if (have_posts()) : ?>
<?php
while( $wp_query->have_posts() ) {
the_post();
ldl_get_template_part('listing', 'compact');
ldl_get_featured_posts();
}
?>
<?php else : ?>
<?php endif; ?>
You are almost there
If you change the choices in the Advanced Custom Fields to
1 : Free
2 : Business
3 : Commercial
4 : Professional
5 : Ultimate
6 : Ultimate Plus
And then the default to 1
By doing this you are setting the values to the number and the default is the Free value. In the admin screen you will be presented with the text value.
Then to do your query try this for the query
$wp_query = get_posts(array(
'numberposts' => -1,
'post_type' => 'directory_listings',
'meta_key' => 'membership_type',
'orderby' => 'meta_value',
));
It will get all of the posts that have a value set and order it by the membership type descending which is what you want.
I have tried this on my local setup to confirm this.
I have a wp website with lunch and dinner offers for the next day. The offers are entered as posts, are in there own category (id=4) and have two custom field value:
- menu_date - the date when the post should be visible
- menu_number - if it's lunch it's 1, if it's dinner is 2
I use this query to get the posts i need:
$args = array(
'post_type' => 'post',
'cat' => 4,
'meta_key'=>'menu_number', 'orderby' => 'meta_value_num', 'order' => ASC );
$query = new WP_Query($args);
if ( $query->have_posts() ) {
while($query->have_posts()) {
$query->the_post();
$menu_date = get_post_custom_values( 'menu_date' );
$menu_number = get_post_custom_values( 'menu_number' );
$tommorow = date("dmY", time()+86400);
if ( $tommorow == $menu_date[0] && has_post_thumbnail() ) {
the_post_thumbnail('full', array( 'class' => 'img-responsive' ) );
if ($menu_number[0] == 1) {
Get Lunch!
}
else{
Get Dinner!
}
the_title();
}
}
} wp_reset_query();
Every day both lunch and dinner offers should be visible, but sometimes only lunch is shown, but after a few refreshes both are visible again.
Any way to improve the code so that this doesn't happen anymore? Thanks
Every day both lunch and dinner offers should be visible, but sometimes only lunch is shown, but after a few refreshes both are visible again.
It may be related to how you compute for tomorrow's date:
$tommorow = date("dmY", time()+86400);
Try changing it to as follows and see if that would work:
$tomorrow = date("dmY", strtotime("tomorrow"));
Try to check both at once in a loop. Like this :
`if ($menu_number[0] == 1) {
Get Lunch!
}
if ($menu_number[0] == 2){
Get Dinner!
}`
If this doesn't solve the problem, try debugging the values of $menu_number and $menu_date. See what is coming.
EDIT:
The meta query args should be like this: $tommorrow was incorrect in your args
'meta_query' => array(
array(
'key' => 'menu_date',
'value' => $tommorow
)
)
For Date Query:
$tomorrow = date("dmY", time()+86400);
$args = array(
'date_query' => array(
array(
'year' => $tomorrow["year"],
'month' => $tomorrow["mon"],
'day' => $tomorrow["mday"],
),
),
);
This will give you tomorrow's posts.
I figured it out:
I changed the $args like this. I added the menu_date here and I'm only retrieving the posts that have correct value.
$args = array(
'post_type' => 'post',
'cat' => 4,
'meta_query' => array(
array(
'key' => 'menu_date',
'value' => "$tommorow"
)),
'meta_key'=>'ordine_meniu', 'orderby' => 'meta_value_num', 'order' => ASC );
$query = new WP_Query($args);
I'm creating a custom PHP script that adds posts to WordPress and allows you to view/modify posts.
The WordPress Theme I'm using has specific post_type, and specific variables to that type.
I can add posts without any issues, but I'm having a difficult time trying to query all posts with the specific tax_input value.
Here is the code that adds posts:
include('../wp-config.php'); //Get WordPress Config
$new_listing = array(
'post_title' => $listing_title,
'post_name' => str_replace('-', ' ', $listing_title),
'post_content' => $listing_description,
'tax_input' => array('property-status' => $listing_phase),
//$listing_phase is the `term_id` number from what I see in the database
'post_status' => 'publish',
'post_type' => 'property',
'post_author' => 1
);
$listing_id = wp_insert_post($new_listing); //Insert the post into the database
add_post_meta($listing_id, 'REAL_HOMES_banner_sub_title', $listing_subtitle);
add_post_meta($listing_id, 'REAL_HOMES_property_address', $listing_address);
add_post_meta($listing_id, 'REAL_HOMES_property_location', $listing_address_lat.','.$listing_address_lng);
add_post_meta($listing_id, 'REAL_HOMES_property_size', $listing_sqare_foot);
add_post_meta($listing_id, 'REAL_HOMES_property_size_postfix', 'Sq Ft');
add_post_meta($listing_id, 'REAL_HOMES_property_bedrooms', $listing_bedrooms);
add_post_meta($listing_id, 'REAL_HOMES_property_bathrooms', $listing_bathrooms);
add_post_meta($listing_id, 'REAL_HOMES_property_garage', $listing_garage);
What do I need to do to get posts with the same tax_input value?
The below code gets all the properties but with all property-status values, I want to show only a certain properties with certain property-status values:
$postArgs = array('posts_per_page' => 25, 'post_type' => 'property');
$getListings = get_posts($postArgs);
foreach($getListings as $post) : setup_postdata($post);
?>
<?=the_title()?>
<div class="content"><p><?=the_content()?></p></div>
<?
endforeach;
wp_reset_postdata();
Thanks in advance!
I have resolved my issue once I found the Wordpress documentation for taxonomies.
http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters
The working code:
$postArgs = array(
'posts_per_page' => 25,
'post_type' => 'property',
'tax_query' => array(
array(
'taxonomy' => 'property-status', //Tax_Input
'field' => 'term_id', //Or Slug
'terms' => '48' //Term ID or Slug Name
)
)
);
$getListings = get_posts($postArgs);
foreach($getListings as $post) : setup_postdata($post);
?>
<a>" class="deploy-toggle-1"><?=the_title()?></a>
<div class="content"><p><?=the_content()?></p></div>
<?
endforeach;
wp_reset_postdata();
Enjoy!