I try to show posts before today in DESC order in Wordpress.
I got this:
<?php query_posts( array( 'cat' => '2186', 'paged' => get_query_var('paged'), 'order' => 'DESC', 'before' => 'strtotime("now")' ) ); ?>
I know I should use the before parameter if I refer to the Wordpress documentation.
Actually, this code doesn't work. What should I try?
Try this, works, and it's easy to modify:
$args = array (
'cat' => '2186',
'paged' => get_query_var('paged'),
'order' => 'DESC',
'paged' => $paged,
'date_query' => array(
array(
'before' => array(
'year' => date('Y'),
'month' => date('m'),
'day' => date('d')
),
'inclusive' => true,
),
),
);
query_posts($args);
Related
I'm banging my head against the wall with this, is there anything wrong with my code?
I want to query all posts that have 'launch_date' today and beyond.
<?php
// find date time now
$date_now = date('M, d');
// query events
$args = array(
'posts_per_page' => 20,
'post_type' => 'project',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'launch_date',
'compare' => '<=',
'value' => $date_now,
'type' => 'DATETIME'
)
),
'order' => 'DESC',
'orderby' => 'meta_value',
'meta_key' => 'launch_date',
'meta_type' => 'DATE'
);
echo build_query( $args );
?>
I have the Wordpress theme called Clipper with Flatter as the child theme.
The theme uses query_output to output results in the homepage. My intention is to sort by custom fields, 'clr_up_vote'. However, this caused the outputs to be reduced to only posts with 'clr_up_vote' data and hid the rest.
I initially set out to fix by having two queries in index.php,
$ordered_posts = query_posts(array(
'post_type' => APP_POST_TYPE
, 'ignore_sticky_posts' => 1
, 'paged' => $page
, 'posts_per_page' => 10
, 'offset' => $offset
,'meta_key' => 'clpr_votes_up',
'orderby' => 'meta_value',
'order' => 'DESC',
'meta_query' => array(
array(
'key' => 'clpr_votes_up',
'meta-value' => $value,
'value' => 1,
'compare' => '>='
),
array(
'key' => 'clpr_votes_down',
'value' => '',
'compare' => 'LIKE'
)
)
));
get_template_part('loop', 'coupon');
And,
$unordered_posts = query_posts(array(
'post_type' => APP_POST_TYPE
, 'ignore_sticky_posts' => 0
, 'paged' => $page
, 'posts_per_page' => 10-count($ordered_posts)
, 'offset' => $offset
, 'post__not_in' => $post_ids
));
get_template_part('loop', 'coupon');
But now the pagination doesn't seem to work. I tried to use WP_Query but the theme will only accept query_posts().
Any suggestions will be great. Thanks!
Remove meta query from query posts. Just orderby should be set to meta value and meta key should be set to the clpr_votes_up.
$ordered_posts = query_posts(array(
'post_type' => APP_POST_TYPE
, 'ignore_sticky_posts' => 1
, 'paged' => $page
, 'posts_per_page' => 10
, 'offset' => $offset
,'meta_key' => 'clpr_votes_up',
'orderby' => 'meta_value',
'order' => 'DESC'
));
And if you want all the posts then remove posts_per_page from the query.
I need to exclude a category from showing up posts.
I registered the taxonomy: portfolio-category
and added a category: accessories (cat ID 19) under portfolio-category
How do I exclude posts from accessories category from showing up?
I tried: 'category' => -19, but it didn't work
here's my code:
<?php
$args=array(
'post_type' => 'items',
'post_status' => 'publish',
'showposts' => intval( get_anolox_option_by('an_homep_count', 3) ),
'caller_get_posts' => 1,
'category' => -19,
'paged' => $paged,
);
query_posts($args);
$end = array(3,6,9,12,15,18,21,24,27,30,33,36,39,42,45);
$i = 0;
while (have_posts()): the_post();
global $post;
$i++;
?>
MY CODE HERE, NO NEED TO SHOW SINCE IT'S VERY LONG
<?php endwhile; ?>
<?php wp_reset_query(); ?>
edit// I tried this code, but still it didn't work:
<?php
$args=array(
'post_type' => 'items',
'post_status' => 'publish',
'showposts' => intval( get_anolox_option_by('an_homep_count', 3) ),
'caller_get_posts' => 1,
'paged' => $paged,
'tax_query' => array(
'taxonomy' => 'portfolio-category',
'terms' => 'accessories',
'field' => 'slug',
'operator' => 'NOT IN')
);
query_posts($args);
$args = array(
'post_type'=>'items',
'order'=>'ASC',
'posts_per_page'=>3
'tax_query' => array(
array(
'taxonomy' => 'portfolio-category',
'field' => 'id',
'terms' => 19,
'operator' => 'NOT IN',
),
)
));
query_posts($args);
items= custom post type
portfolio-category = my custom taxonomy
for multiple category exclude use 'terms' => array( '19,20' ),
The category argument is meant for the built in category taxonomy. Change your $args like so to reference your custom taxonomy:
$args=array(
'post_type' => 'items',
'portfolio-category' => 'accessories',
'post_status' => 'publish',
'showposts' => intval( get_anolox_option_by('an_homep_count', 3)),
'paged' => $paged
);
This assumes the following:
You've got a custom post type called items.
The portfolio-category taxonomy is registered to it.
accessories is added to the portfolio-category taxonomy.
Update: Whoops...getting late. To answer OP's actual question of how to exclude the accessories portfolio category (rather than include it as the above does), you can use the tax_query argument. Code would be as follows to exclude accessories:
$args=array(
'post_type' => 'items'
'post_status' => 'publish',
'showposts' => intval( get_anolox_option_by('an_homep_count', 3)),
'paged' => $paged,
'tax_query' => array(
'taxonomy' => 'portfolio-category',
'terms' => 19,
'field' => 'id',
'operator' => 'NOT IN'
)
);
The issue seems to be a layer of nesting. Try changing
$args=array(
'post_type' => 'items'
'post_status' => 'publish',
'showposts' => intval( get_anolox_option_by('an_homep_count', 3)),
'paged' => $paged,
'tax_query' => array(
'taxonomy' => 'portfolio-category',
'terms' => 19,
'field' => 'id',
'operator' => 'NOT IN'
)
);
To:
$args=array(
'post_type' => 'items'
'post_status' => 'publish',
'showposts' => intval( get_anolox_option_by('an_homep_count', 3)),
'paged' => $paged,
'tax_query' => array(
array(
'taxonomy' => 'portfolio-category',
'terms' => 19,
'field' => 'id',
'operator' => 'NOT IN'
)
)
);
This worked for me. :-/
Something weird happening in my query and can't see what's wrong
I am passing the variable THEME from a select list.
THe theme is pulled from the taxonomy THEME
so my code looks like
$thetheme = $_GET['theme'];`
$thetheme is correctly passed from URL
then
$args2 = array(
'tax_query' => array(
array(
'taxonomy' => 'theme',
'field' => 'slug',
'terms' => $thetheme
)
),
'post_type' => array( 'post', 'dvd' ),
'cat' => '31',
'paged' => $paged,
'posts_per_page' => $listitems,
'order' => 'DESC',
'orderby' => 'date',
'query' => $wp_query
);`
The query is working only on some Post, not all of them, and cant understand why.
If I select a post with the Theme "Adventure" for example, it will pull the correct amount of post.
But an other post, in the same category with a different theme, will not be displayed.
This is puzzling me....
HElP!
thx
Don't think you need here to run complete taxonomy query... try this =)
$args2 = array(
'theme' => $thetheme,
'post_type' => array( 'post', 'dvd' ),
'cat' => '31',
'paged' => $paged,
'posts_per_page' => $listitems,
'order' => 'DESC',
'orderby' => 'date',
'query' => $wp_query
);
<?php query_posts(array('showposts' => 1000, 'post_parent' => $post->ID, 'post_type' => 'page', 'orderby' => 'title', 'order' => 'ASC', 'meta_key' => featured_product, 'meta_value' => 1)); ?>
<?php query_posts(array('showposts' => 1000, 'post_parent' => $post->ID, 'post_type' => 'page', 'orderby' => 'title', 'order' => 'ASC')); ?>
I have 2 queries, first to show meta key with featured_product eq 1. So I want to exclude all the featured products on the second query. How can i do that please? Thanks!
You simply need the meta_compare for that:
<?php query_posts(
array(
'showposts' => 1000,
'post_parent' => $post->ID,
'post_type' => 'page',
'orderby' => 'title',
'order' => 'ASC',
'meta_key' => 'featured_product',
'meta_compare' => '!=',
'meta_value' => 1
)
); ?>
While looping through your first query, build an array consisting of the ids of the posts in the loop. Then use pass this array to the second query using "post__not_in" => $your_array