WP_Query for Archive Page Date not work - wordpress

I have edited my theme on archive.php for show only a specific post and exclude a post with specific meta_key:
$posts_to_exclude_args = array(
'meta_key' => 'vip_box',
);
$posts_to_exclude = new WP_Query( $posts_to_exclude_args );
$to_exclude = array();
while ( $posts_to_exclude->have_posts() ) : $posts_to_exclude->the_post();
$to_exclude[] = $post->ID;
endwhile;
wp_reset_postdata();
$lastupdated_args = array(
'post__not_in' => $to_exclude,
'author__in' => $author,
'category__in' => $terms,
'posts_per_page' => 12,
'has_archive' => true,
'paged' => $paged,
);
$lastupdated_loop = new WP_Query( $lastupdated_args );
query_posts( $lastupdated_args );
<?php if ( have_posts() ) : ?>
And its perfect but now if i open a date link/url mywebsite.com/2017/06 it show all post and not only post in this date, why?.
Please can you help me?

I have fixed with this code, but i think there is a best solution than this. =)
$year = get_query_var('year');
$monthnum = get_query_var('monthnum');
$day = get_query_var('day');
'date_query' => array(
array(
'year' => $year,
'monthnum' => $monthnum,
'day' => $day,
),
),
);

Related

Filtered a query in WordPress

To display some items from a post type, I'm using this WordPress query:
$posts = get_posts(array(
'post_type' => 'realisations',
'status' => 'publish',
'order' => 'ASC'
));
But how can I filter the datas returned by this query depending the infos in the post type page ? For example, I have a input 'year' to get the year of the project.
Thanks.
You can use wp_query like Below
$args = array (
'post_type' => array( 'realisations' ),
'post_status' => array( 'publish' ),
'order' => 'ASC',
'orderby' => 'date',
'year' => 'yourinputyear' // 2021
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) { ?>
<?php while ( $query->have_posts() ) : $query->the_post();
echo get_the_title();
endwhile;
}else{
echo "Data not found";
}
Please try this way. Hope is useful.
Thanks
You can use Date Parameters check below code.
$posts = get_posts( array(
'post_type' => 'realisations',
'status' => 'publish',
'order' => 'ASC',
'date_query' => array(
array( 'year' => 'yourinputyear' )
)
) );

Displaying posts by two category in WordPress?

I want posts that belong to 2 categories to be shown under my current posts, and the 2 categories must be determined by the current post that been displayed, so I write this code but not working, please help, tnx.
<?php
$categories = get_the_category();
$category_id = $categories[0]->cat_ID;
$category_id2 = $categories[1]->cat_ID;
$query = new WP_Query( array('category__and' => ($category_id, $category_id2),
'post_type' => 'post',
'posts_per_page' => 4,
'orderby' => 'publish_date',
'order' => 'DESC',);
while($query->have_posts()) : $query->the_post();
?>
It throws a php error because you forgot a ) at the end.
This shoud work:
$categories = get_the_category();
$category_id = $categories[0]->cat_ID;
$category_id2 = $categories[1]->cat_ID;
$query = new WP_Query(
array(
'category__and' => array( $category_id, $category_id2 ),
'post_type' => 'post',
'posts_per_page' => 4,
'orderby' => 'publish_date',
'order' => 'DESC',
),
);
while($query->have_posts()) : $query->the_post();
Try to use wp_debug in such cases

wordpress custom post type loop by tag id

I'm trying to make a loop of a custom type of posts by tag ID.
This is the original code:
<?php $args = array(
'post_type' => 'kana_portfolio',
'meta_key' => 'choose_layout_2',
'meta_value' => 'layout-1',
'posts_per_page' => -1
);
$posts = get_posts($args);
$term_array = array();
$portfolio_term_array = array();
foreach ( $posts as $post ) : setup_postdata( $post );
$terms = wp_get_post_terms( get_the_ID(), 'kana_genre');
foreach($terms as $term){
$term_array[$term->slug] = $term->name;
$portfolio_term_array[] = $term->slug;
}
endforeach;
wp_reset_postdata(); ?>
Since I need to loop only the posts in a certain category (of custom posts type) having id 31 I added "'tag_id' => 31" after "'posts_per_page' => -1"
<?php $args = array(
'post_type' => 'kana_portfolio',
'meta_key' => 'choose_layout_2',
'meta_value' => 'layout-1',
'posts_per_page' => -1,
'tag_id' => 31
);
$posts = get_posts($args);
$term_array = array();
$portfolio_term_array = array();
foreach ( $posts as $post ) : setup_postdata( $post );
$terms = wp_get_post_terms( get_the_ID(), 'kana_genre');
foreach($terms as $term){
$term_array[$term->slug] = $term->name;
$portfolio_term_array[] = $term->slug;
}
endforeach;
wp_reset_postdata(); ?>
[EDIT]
I have another loop below in the page and I tried to insert 'cat'=>31 tax_query array
<?php $number_posts_to_display = get_field('number_of_posts_to_display');
$display_order = get_field('post_display_order');
$portfolio = array(
'post_type' => 'kana_portfolio',
'posts_per_page' => $number_posts_to_display,
'order' => $display_order,
'meta_key' => 'choose_layout_2',
'meta_value' => 'layout-1',
'tax_query' => array(
array(
'taxonomy' => 'kana_genre',
'field' => 'slug',
'terms' => $portfolio_term_array,
),
),
);
$portfolio_loop = new WP_Query($portfolio); ?>
But nothing is showed, how can I do?
if your category (we are talking real categories here, of the taxonomy type category...) is 31, you should use 'cat'=>31, tags are tags, they are not categories.. if this helped, let me know. if you were trying to filter on tags and it was not working, let me know as well and ill take a look in depth.
Have fun!
Instead of:
'tag_id' => 31
Try this:
'tag__in' => 31

custom post type loop not working

I am trying to create a custom loop from the metadata I have entered into the posts.
<?php $args = array (
'post_type' => array( 'movies' ),
'order' => 'DESC',
'order_by' => 'get_post_meta( get_the_ID(), "released", true )',
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) :
bd_pagination();
while ( $query->have_posts() ) : $query->the_post(); ?>
I so so need help plz
You need to tweak your parameters a bit. I'm assuming the "released" is a date field? If not (eg, if it's a timestamp), use a numeric orderby instead. But this should set you on your way:
<?php
$args = array(
'post_type' => array( 'movies' ),
'orderby' => 'meta_value_date',
'meta_key' => 'released',
'meta_type' => 'DATE'
);
$query = new WP_Query( $args );

Wordpress Custom Post Meta Query

I'm trying query a custom post type for each comment with the dynamic field "comment_ID." I'm using the code below. This currently shows the comment ID, which I don't want, but does not show 'paid' as I would like.
<?php
$commID = comment_ID();
$args = array( 'post_type' => 'paidbriefs', 'meta_key' => 'Comment_ID', 'meta_value' => 'echo $commID', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
echo 'paid';
endwhile; ?>
</p>
I'm obviously doing something wrong with echoing the $commID variable as this does not show anything. If I change this to just $commID it returns 'paid' for every comment that has a Comment_ID meta, regardless of whether it matches the actual comment ID. Does anyone know how to fix this?
I think what you will need to do is a Loop, within a loop,
first loop to generate the standard loop, this will have your post info, comments etc.
within that loop you need to declare your comment_ID;
then from there, you setup another internal loop,
using the comment_ID for your custom field,
<?php
$args = array( 'post_type' => 'paidbriefs', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$commID = comment_ID();
$innerargs = array( 'post_type' => 'paidbriefs',
'meta_key' => 'Comment_ID',
'meta_value' => $commID,
'posts_per_page' => 10 );
$innerloop = new WP_Query( $innerargs );
while ( $innerloop ->have_posts() ) : $innerloop ->the_post();
echo 'paid Comment';
endwhile;
endwhile;
?>
untested though.
hopefully this will help,
<?php
$args = array( 'post_type' => 'ait-dir-item',
'meta_query' => array(
array(
'key' => 'location',
'value' => 'annapolis'
),
array(
'key' => 'item_tags',
'value' => 'non-marine'
)
),
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page' => 300 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
the_title('<h3 class="entry-title">', '</h3>');
echo '<div class="entry-content">';
the_content();
echo '</div>';
endwhile;?>
you can try this one

Resources