Title: Query week filter
Last modified: August 21, 2016

---

# Query week filter

 *  [efishop](https://wordpress.org/support/users/efishop/)
 * (@efishop)
 * [12 years, 2 months ago](https://wordpress.org/support/topic/query-week-filter/)
 * Hello all,
    I have the fallowing php code:
 *     ```
       $my_query = new WP_Query('category_name=Citat&showposts=1&orderby=rand'); ?>  
   
       <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
         <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php echo get_post_meta( get_the_ID(), 'citat', true ); ?></a><br/>
       <div class="autor_citat"><?php echo get_post_meta( get_the_ID(), 'autor', true ); ?></div>
       <?php endwhile;
       ```
   
 * How can I add a function that display only the displays post from current week?
 * I have tried this: [http://codex.wordpress.org/Template_Tags/query_posts#Time_Parameters](http://codex.wordpress.org/Template_Tags/query_posts#Time_Parameters)
   but with no luck, it display tree posts instead of one.

Viewing 5 replies - 1 through 5 (of 5 total)

 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [12 years, 2 months ago](https://wordpress.org/support/topic/query-week-filter/#post-4797906)
 * I suggest you convert entirely to the preferred array format, and update to ‘
   posts_per_page’ instead of ‘showposts’. Once you combine your new format with
   the current week example from the Codex, you end up with this:
 *     ```
       $args = array(
        	'category_name' => 'citat', //must be slug, not name!
       	'posts_per_page' => 1,
       	'date_query' => array(
       		array(
       			'year' => date('Y'),
       			'week' => date('W'),
       		),
       	),
       	'orderby' => 'rand',
       );
       $my_query = new WP_Query( $args );
       ```
   
 * When you run the loop with this, I think you will get what you want. Note the
   category “name” actually requires a **slug**.
 *  Thread Starter [efishop](https://wordpress.org/support/users/efishop/)
 * (@efishop)
 * [12 years, 1 month ago](https://wordpress.org/support/topic/query-week-filter/#post-4797961)
 * Thank you bcworkz,
    I added your code but now I get a “Parse error: syntax error,
   unexpected ‘<‘ ” error. And I can not identify the issue. Can u help with the
   entire loop?
 *     ```
       $args = array(
        	'category_name' => 'citat',
       	'posts_per_page' => 1,
       	'date_query' => array(
       		array(
       			'year' => date('Y'),
       			'week' => date('W'),
       		),
       	),
       	'orderby' => 'rand',
       );
       $my_query = new WP_Query( $args );
   
       <?php while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
       <a>" title="<?php the_title(); ?>"><?php echo get_post_meta( get_the_ID(), 'citat', true ); ?></a>
       <div class="autor_citat"><?php echo get_post_meta( get_the_ID(), 'autor', true ); ?></div>
       <?php endwhile;
       ```
   
 * Thank you.
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [12 years, 1 month ago](https://wordpress.org/support/topic/query-week-filter/#post-4797981)
 * Sure, there’s a couple problems, nothing major. First of all, you may need to
   add `<?php` above the `$args`, or possibly not, depending on what is above. If
   HTML, then it is needed, if more code without a terminating `?>`, it is not.
 * For the same reason, the `<?php` between the new WP_Query line and the start 
   of the while loop needs to be deleted. The text before it is code without a terminating`?
   >`, not HTML.
 * The cause of the error message is the next line, the anchor tag is malformed 
   and has no href attribute. Try replacing that line with this:
    `<a title="<?php
   the_title(); ?>" href="<?php the_permalink(); ?>"><?php echo get_post_meta( get_the_ID(),'
   citat', true ); ?></a>`
 * I’m pretty sure that’s right, but that syntax is inherently difficult to proofread.
   Even though it’s more code, I prefer doing it this way:
 *     ```
       <?php $title =  get_the_title(); //note use of non-echoing versions
       $link_url = get_permalink();
       $link_text = get_post_meta( get_the_ID(), 'citat', true );
       echo "<a title=\"$title\" href=\"$link_url\">$link_text</a>"; ?>
       ```
   
 * Especially when using a syntax highlighting editor, this version is much easier
   to read and understand. I avoid going back and forth between code and HTML unless
   they’re in substantive blocks.
 *  Thread Starter [efishop](https://wordpress.org/support/users/efishop/)
 * (@efishop)
 * [12 years, 1 month ago](https://wordpress.org/support/topic/query-week-filter/#post-4798050)
 * Hello bcworkz and thank u very much for your time and for your help. It seems
   that u are a wizard of php since u speak such a strange language 🙂
 * I have used this:
 *     ```
       <?php
       $args = array(
        	'category_name' => 'citat-newsar', //must be slug, not name!
       	'posts_per_page' => 1,
       	'date_query' => array(
       		array(
       			'year' => date('Y'),
       			'week' => date('W'),
       		),
       	),
       	'orderby' => 'rand',
       );
       $my_query = new WP_Query( $args ); ?>
   
       <?php $title =  get_the_title(); //note use of non-echoing versions
       $link_url = get_permalink();
       $link_text = get_post_meta( get_the_ID(), 'citat', true );
       echo "<a title=\"$title\" href=\"$link_url\">$link_text</a>"; ?>
       ```
   
 * everything works ok put it works only on category page and on post page from 
   witch that custom fields are. We have some custom fields “citat” and “autor” 
   when we publish a post we select some words that goes in “citat” and an author
   witch goes in “autor”. Than in website header we randomly display those custom
   fields.
    Our old function works ok but we need a way to display only custom fields“
   citat” and “autor” from last week.
 * Any sugestion?
    And a pay pal account to make u a small donation? (I hope this
   is not against wp policy).
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [12 years, 1 month ago](https://wordpress.org/support/topic/query-week-filter/#post-4798053)
 * Actually, offering or accepting payment in these forums is against the rules.
   While I appreciate your willingness to pay, it is not necessary. I am rewarded
   by helping people here in ways that are independent of monetary reward.
 * While I “speak” PHP, I am no wizard. My “vocabulary” is limited, so I avoid writing
   code for people. I usually either rework existing code as I did for you, or just
   suggest in general terms a proper approach. While a PHP solution exists for what
   you want, it is better done with mySQL, which I don’t “speak” well at all. You
   basically would want to query for distinct entries in the meta_value column of
   the postmeta table where (meta_key = ‘citat’ or meta_key = ‘autor’) and where
   through various table joins, the associated post has a published date within 
   the last week. I know better than to even try to put that in proper mySQL syntax,
   it would be nowhere close to correct. I know the logic is correct though.
 * While this can also be done in PHP, it is nowhere near as efficient as using 
   mySQL. If the data volume is not too much, it can be a workable solution, and
   something I know how to do much better than with mySQL.
 * You first query for all posts within the last week instead of just one, simply
   by changing the posts per page to -1 instead of 1. If all posts in that category
   have these custom fields, there is no need to further qualify the query. If some
   posts do not qualify, you need to add in a meta_query array argument to limit
   posts returned to those with the required custom fields.
 * You then loop through the posts just as you might for an archive page, except
   instead of outputting the titles and permalinks, etc. you push each meta value
   into it’s related array collection of values, but before actually adding to the
   array, first check to see if the value is already in the array with `in_array()`,
   in which case there is no need to push the value in again.
 * Once the arrays of unique values are collected, use `join()` to splice the values
   together in a format that is suitable for output.

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Query week filter’ is closed to new replies.

## Tags

 * [filter](https://wordpress.org/support/topic-tag/filter/)
 * [query](https://wordpress.org/support/topic-tag/query/)
 * [week](https://wordpress.org/support/topic-tag/week/)

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 5 replies
 * 2 participants
 * Last reply from: [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * Last activity: [12 years, 1 month ago](https://wordpress.org/support/topic/query-week-filter/#post-4798053)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
