Query post with current date month with custom field? - wordpress

How to query Post with current date month with custom field:
Here is my code
<?php
global $wp_query;
$event_month = get_post_meta($wp_query->post->ID, 'eventdate', true); //format in db 11/15/2010
$event_month = date("n"); //A numeric representation of a month, without leading zeros (1 to 12)
$today= getdate(); ?>
<?php query_posts('meta_key='.$event_month .'&meta_compare&meta_value=' .$today["mon"]);?>
<?php while (have_posts()): the_post(); ?>
<div class="event-list-txt">
<h4><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>" rel="bookmark">
<?php the_title(); ?>
</a></h4>
<?php the_content(); //Display the content?>
</div>
<?php endwhile; ?>
<?php wp_reset_query();?>
but it doesn't display the output.. .what I want to display is the event list for the month.. for example if it's November it will display the event list for November and when the month of December comes it will show the event list of December and so on.. . The event list date comes with custom field format like this 11/15/2010 compare to current date
i'm stack on it.. .thanks guys
here are the additional expected output
current date is November and the event list should be like this:
+-----------------------+-------------+
| Event Name | Date |
+-----------------------+-------------+
| ABC Market opening | 11/18/2010 |
| ABC Market Seminar | 11/25/2010 |
| ABC Market Promo | 11/29/2010 |
+-----------------------+-------------+

I think your problem is here:
<?php query_posts('meta_key='.$event_month .'&meta_compare&meta_value=' .$today["mon"]);?>
...You're trying to find a custom field named "11" (Numerical representation of November) with value 11.
Check http://codex.wordpress.org/Function_Reference/query_posts#Time_Parameters for a code snippet that allows you to set the "WHERE" part of the query:
Return posts for posts for March 1 to March 15, 2009:
<?php
//Create a new filtering function that will add our where clause to the query
function filter_where($where = '') {
//posts for March 1 to March 15, 2009
$where .= " AND post_date >= '2009-03-01' AND post_date < '2009-03-16'";
return $where;
}
// Register the filtering function
add_filter('posts_where', 'filter_where');
// Perform the query, the filter will be applied automatically
query_posts($query_string);
?>
Hope this helps at all...

Related

Wordpress: Get post date on Category page

On Category page displaying post of particular category with title and post date. The issue is that it is getting the date for only the first list item while for the other list item it returns blank.
Here's my code:
<div class="post-date">
<?php the_date(); ?>
</div>
How can i display post published date with each post title?
Use this
<?php the_time('F j, Y \a\t g:i a'); ?> or
<?php the_time(get_option('date_format')); ?>
When there are multiple posts on a page published under the SAME DAY, the_date() only displays the date for the first post (that is, the first instance of the_date()). To repeat the date for posts published under the same day, you should use the Template Tag the_time() with a date-specific format string.
you may try the follow to get the post date
<?php the_time( get_option( 'date_format' ) ); ?>

How do I display custom fields in Wordpress in alphabetical order?

I need to display a custom field on a WP page that contains only values beginning with the letter A, without duplicates. Currently, I am using the code that was given earlier by Soju. This sorts alphabetically, but I can't get it to display only values beginning with one letter:
<?php
$movie_reviews = get_posts(
'numberposts=-1&order=ASC&orderby=meta_value&meta_key=LAST%20NAME' );
$directors = array();
foreach( $movie_reviews as $post ) {
$director = get_post_meta( $post->ID, 'LAST NAME', true );
}
$directors = array_unique($directors);
foreach( $movie_reviews as $post ) : setup_postdata( $post );
foreach ($directors as $director) {
}
if (strtolower($director[0])=='') {
// display what you want
}
?>
<span><li>
<?php $director = get_post_meta( $post->ID, "LAST NAME", $single = true );
if( $director !== '' ) {
echo $director;
} ?></br></li>
</span>
<?php endforeach; ?>
I think you'll be better off handling this in the database query rather than with PHP. With your current code you will make dozens of database calls which is inefficient.
Pulling the Records
WordPress stores all the custom fields in the wp_postmeta table and can be retrieved as follows.
mysql> SELECT post_id, meta_value FROM wp_postmeta WHERE meta_key = 'LAST NAME';
+---------+-------------------+
| post_id | meta_value |
+---------+-------------------+
| 4 | Spielberg |
| 6 | AveryTiredman | <-- in the list twice
| 8 | bLanguage |
| 10 | aWakeMahn |
| 12 | dScreensAreBrukin |
| 14 | Aluloby |
| 16 | AaLetterC |
| 19 | AveryTiredman | <--
+---------+-------------------+
8 rows in set (0.00 sec)
I've created multiple posts with the custom field of LAST NAME to replicate your website. As you can see the query above pulls all the last names that I've entered as custom fields.
You can filter and sort the results with the following.
mysql> SELECT meta_value AS last_name FROM wp_postmeta
WHERE meta_key = 'LAST NAME' and meta_value LIKE 'a%'
GROUP BY meta_value
ORDER BY meta_value;
+---------------+
| last_name |
+---------------+
| AaLetterC |
| Aluloby |
| AveryTiredman | <-- only shown once
| aWakeMahn |
+---------------+
4 rows in set (0.00 sec)
As you can see the above query only pulls unique records, that start with the letter "a", and it orders them by name.
Note: I changed the column name to be more relevant with SELECT meta_value AS last_name.
Using with WordPress
WordPress has its own database class with a method called get_results() that let's you run your own query. The method will return an array of objects that will let you access the column names.
<?php $directors = $wpdb->get_results("SELECT meta_value AS last_name FROM wp_postmeta WHERE meta_key = 'LAST NAME' and meta_value LIKE 'a%' GROUP BY meta_value ORDER BY meta_value") ?>
<ol>
<?php foreach( $directors as $director ) : ?>
<li><?php echo $director->last_name ?></li>
<?php endforeach ?>
</ol>
I know that you're new to this and I tried to lay it out so you can understand what's going on. If you have any questions just let me know and I'll try to help you out.
Update
The easiest way to make a page with links to filter through directors is with a custom page template. Basically, create a file named page-directors.php with the following content. Then add a new page and set the template to "Directors Page".
<?php
/**
* Template Name: Directors Page
*/
// get a distinct set of the first letters available
$letters = $wpdb->get_results("SELECT DISTINCT substring(meta_value, 1, 1) as letters FROM wp_postmeta WHERE meta_key = 'LAST NAME' ORDER BY meta_value", ARRAY_N);
// get the letter from the URL
// and make sure there is only a single letter
$director_letter = filter_input(INPUT_GET, 'director', FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => '/^[a-z]$/i')));
// set a default if none is available
if( $director_letter == false ) {
$director_letter = 'a';
}else {
$director_letter = strtolower($director_letter);
}
// don't trust user's input
$director_letter = mysql_real_escape_string($director_letter);
// same query as before but includes letter from user
$directors = $wpdb->get_results("SELECT meta_value AS last_name FROM wp_postmeta WHERE meta_key = 'LAST NAME' and meta_value LIKE '$director_letter%' GROUP BY meta_value ORDER BY meta_value");
get_header();
?>
<div id="primary">
<div id="content" role="main">
<?php while ( have_posts() ) : the_post(); ?>
<!--
CSS:
#director_letters li {
list-style: none;
display: inline;
}
-->
<ol id="director_letters">
<?php foreach( $letters AS $letter ) : ?>
<li><?php echo strtoupper($letter[0]) ?></li>
<?php endforeach ?>
</ol>
<h2>Directors</h2>
<ol>
<?php foreach( $directors AS $director ) : ?>
<li><?php echo $director->last_name ?></li>
<?php endforeach ?>
</ol>
<?php endwhile; // end of the loop. ?>
</div>
</div>
<?php get_footer() ?>
Update #2
If you take the code and SQL queries I've written for you already you should be able to figure it out. I don't want to just give you the answer because you won't learn that way. So here's what I'll do instead. I'm going to give you the steps that you need to take to accomplish it and let you figure it out. If you have any questions or you get stuck, post a comment and I'll help you out.
You'll need to create another custom page template that will take the director's last name as a GET parameter. It will be exactly the same as what that I've used to show the first letters of the directors last names.
Next, you will need to query the wp_postmeta table and select the post_id WHERE the meta_value equals the director's last name.
You will need to loop over the post ids and display the post's title.
Note: A function that you will find handy is get_the_title(). You can pass the id of a post and it will return the title.
Everything I've shown you so far can be used to answer your question. But, as I said before, if you run into a problem that you can't figure out let me know.
Also, once you've completed this feature please add the code back to your original question. There's a good chance that we can greatly improve the efficiency of it :)
P.S. If you have don't understand some of the code that I've supplied you, please let me know. It won't do you any good to have code that you don't understand :)
Update #3
I made a video explaining all the steps from the beginning. It starts with pulling out the director's last names, then showing all the letters, and finally showing all WordPress posts with the director.

Drupal 7 change Submitted by format

I'm new to drupal but learning quickly. I've got drupal 7 and I'm working on creating a theme based on Zen's starterkit sub-theme. I'm trying to find where I can customize the "Submitted by" line.
The default looks like this:
Submitted by kenny on Sun, 05/13/2012 - 18:33
I'd like to change it so it simply says the date in a nice format (no user name, no time).
Sunday, May 13th, 2012
How and where can I change this?
In your template.php file (of the Zen sub theme), put this.
function mytheme_preprocess_node(&$vars, $hook) {
$vars['submitted'] = date("l, M jS, Y", $vars['created']);
}
If you want to set "Submitted by " and change date formate for specific content type then you can also change your tpl file like
if your content type is "blog"
then create node--blog.tpl.php
<?php if ($display_submitted): ?>
<p class="submitted">
<?php $user = user_load($node->uid); ?>
<?php $username=$user->name; ?>
<?php print t("Written"); ?>
<?php print $username ?>
<time pubdate datetime="<?php print $submitted_pubdate; ?>">
<?php $date = date("d.n.Y", $node->created); ?>
<?php print "<br>".$date; ?>
</time>
</p>
<?php endif; ?>

how to change for the format for the blog archives?

Now the wordpress is displaying the blog archives as following
Janauary 2012
But I want this
Jan (2)
where 2 is the total number of posts in the month of January.
How will I do this?
You have to modify the template and replace this <?php the_time('F Y'); ?> with this <?php the_time('M'); ?> (<?php $current_month = date('m'); $count = get_posts("monthnum=$current_month" ); echo count($count); ?>) and it should work.
WordPress links to archives using the "m" argument to index.php with the year first and the month second. So, for example, January 2012 would be index.php?m=201201. So, to link this, we need to create the link like this:
...

(Wordpress) Only show future posts in archive & search results

I have an events category which is searchable by a sub-category (by city where the event is being held). I have the 'The Future is Now!' plugin to allow me to publish future dated posts but hoping that I can set up a paginated template that, when a post 'expires' it will no longer show up in the loop/archive.
I'm also wondering if you can filter out these posts from search results as well?
This is my current Events page code if this helps:
<h2>Upcoming Events</h2>
<ul class="posts">
<?php
$limit = get_option('posts_per_page');
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts('showposts=' . $limit . '&paged=' . $paged .'&cat=1&order=ASC');
$wp_query->is_archive = true; $wp_query->is_home = false;
?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<li class="events_ticker" id="post-<?php the_ID(); ?>">
<span class="left">
<h3><?php the_time('F jS, Y') ?></h3>
<p><?php if (strlen($post->post_title) > 25) { echo substr(the_title($before = '', $after = '', FALSE), 0, 25) . '...';} else {the_title();} ?></p>
<?php global $more; $more = 0; the_excerpt(); ?>
<p>Read More</p>
</span>
<?php if ( function_exists("has_post_thumbnail") && has_post_thumbnail() ) { the_post_thumbnail(array(143,110), array("class" => "right post_thumbnail")); } ?>
</li>
<img src="<?php bloginfo('stylesheet_directory'); ?>/images/content_breaker_wide.png" alt=" breaker" class="content_breaker" />
<?php endwhile; ?>
</ul>
<!-- end events loop -->
<div class="navigation">
<p class="alignleft"><?php next_posts_link('< Next') ?></p>
<p class="alignright"><?php previous_posts_link('Next >') ?></p>
</div>
<?php endif; ?>
I don't think there is an easy parameter to say "all posts until this date", so you should implement the posts_where_paged filter. There, you can check for !(is_archive() || is_search()), because these two can continue as normal. For the other parts, you add a " AND $wpdb->posts.post_date < NOW()" or something like that (but don't use now, since that will hide events that happen later today, and you probably don't want that).
A similar question was asked on the WordPress Answers Stack Exchange site (in private beta until Aug 20 2010, so you can't visit it unless you pre-registered). Joe Hoyle's suggestion there was simple:
If all you are wanting to do is add an
extra date for 'show times', it may be
easier to add a meta box which does
exactly that - using the Publish date
to spoof it could be potentially
problematic, as WordPress will set it
to "future" status whenever it is
updated with a future publish date (I
think), so you will have to be hooking
every time a post is updated to set it
back again. Also, that way you could
reserve "Publish Date" for what it is
intended.
I would probably just use a meta_key,
and a custom meta box. Though it
depends how complex your queries are
that show the posts.
If you set the meta_value as a
timestamp, you can order by the show
time date still, or select before /
after a certain date:
$events = get_posts('post_type=events&meta_key=show_time&meta_value='
. strtotime( '+1 week' ) .
'&meta_compare=<&orderby=meta_value');
Would get all "events" with a showtime
no later then a week from the current
date. Note: The above is untested, but
should work :)
(This answer is community wiki so I don't get rep points for just reposting what Joe said.)

Resources