To display the post order by modified date in wordpress - wordpress

I have to display the posts oder by last modified date. So I used the code bellow.
$args = array(
'post_type' => $post_type,
'numberposts' => '2',
'orderby' => 'modified',
'order'=> 'ASC',
);
$the_query = new WP_Query( $args );
But I could not find any update in the above code. Should i use something else instead of 'orderby' => 'modified' in the argument.

You should use DESC for order.
Try this:
$the_query = new WP_Query( array(
'post_type' => $post_type,
'numberposts' => '2',
'orderby' => 'modified',
'order' => 'DESC',
));
Using DESC will give you the latest post first(descending order).
EDIT:
As Andrew commented, the default value for order is DESC and can thus be omitted from the code:
$the_query = new WP_Query( array(
'post_type' => $post_type,
'numberposts' => '2',
'orderby' => 'modified',
));

Try
<?php query_posts($query_string . '&post_type=$post_type&orderby=modified&order=desc'); ?>

Related

In WordPress post per page is not working when use exclude ids from the query

I have few feature post which i have to exclude from all post so I have used post__not_in but it return all post instead of posts-per-page 6 post how to fix this isssue.
$exclude_ids = array(1,2,3,4,5,6,7,8);
$args = array(
'post_type' => 'post',
'post__not_in' => $exclude_ids,
'orderby' => 'publish_date',
'order' => 'ASC',
'posts-per-page' => 6
);
$latest_post = new WP_Query( $args );
Maybe its just a typo but the correct param is "posts_per_page" not "posts-per-page" (dash vs underscore) (see here https://developer.wordpress.org/reference/classes/wp_query/#pagination-parameters)
$exclude_ids = array(1,2,3,4,5,6,7,8);
$args = array(
'post_type' => 'post',
'post__not_in' => $exclude_ids,
'orderby' => 'publish_date',
'order' => 'ASC',
'posts_per_page' => 6
);
$latest_post = new WP_Query( $args );

Wordpress wp_query error in marketpress

I am using the following function to get markerpress products in the category 'featured' and display the product images in a slideshow.
$args = array(
'category_name' => 'featured',
'post_type' => 'product'
);
$wp_query1 = new WP_Query( $args );
if (have_posts()) : while ( $wp_query1->have_posts() ) : $wp_query1->the_post()
Problem is that it does not fetch any data. I am forced to just use the below function which displays all the data.
$wp_query1 = new WP_Query('post_type=product');
What am I doing wrong and how can I set limits and also sort the data. Thanks.
<?php $args = array(
'posts_per_page' => 5,
'offset' => 0,
'category_name' => 'featured',
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'product',
'post_status' => 'publish'
);
$posts_array = get_posts( $args ); ?>

WP_Query Search is not working.....for keyword 'TATTOO'

This search is not working.... Any guess?? This is a very simple piece of code.
It is for showing records related with keyword tattoo for title field. It shows all products. The search is not triggered.
$args = array(
'posts_per_page' => $per_page,
'offset' => $paged,
'post_type' => 'product',
'meta_key' => $orderby,
'orderby' => $num_orderby,
'order' => $order,
'search' => '*tattoo*',
'search_columns' => array('post_title')
);
$new_query = new WP_Query( $args );`
http://codex.wordpress.org/Class_Reference/WP_Query#Search_Parameter
try use 's' instead 'search'
...
's' => '*tattoo*',
...

How to query specific custom post with custom fields in a post_type?

I'm new into wordpress and kinda confused how to get 1 post in a custom post. I only knew the loop where it echo all the content in a post_type.
I'm aiming to get 1 post from a post_type 'product-category' and the meta_key is 'product-category-and-type'
You could try this query for custom in wordpress:
$args = array(
'post_type' => 'product-category',
'post_status' => 'publish',
"numberposts" => 1,
'meta_query' => array(
array(
'key' => 'product-category-and-type',
'value' => 'meta_value'
)
)
);
$getPosts = new WP_Query($args);
Just copy and paste below mentioned code at your desired position and replace "your_meta_value" with your actual meta value for meta key "product-category-and-type".
You will get your expected result :
<?php $args = array(
'post_type' => 'product-category',
"numberposts" => 1,
'post_status' => 'publish',
'meta_query' => array(array('key' => 'product-category-and-type','value' => 'your_meta_value'))
);
$myposts = new WP_Query($args);
while($myposts->have_posts()) : $myposts->the_post();
the_title();
endwhile;?>
$args = array(
'post_type' => 'product-category',
'post_status' => 'publish',
'posts_per_page' => '1',
'tax_query' => array(
array(
'taxonomy' => 'product-category-and-type',
'field' => 'slug'
),
),
);
$result = new WP_Query($args);

WP_Query with “post_title LIKE 'something%'” and category

I need to do a WP_Query with a LIKE on the post_title and category
This query_post is not working
query_posts(
array(
'post_type' => 'add_buying',
'like' => $keywords,
'posts_per_page' => 5,
'taxonomy' => 'add_country',
'term' => 'drawing'
));
Check this url and change the like parameter.
query_posts( array(
'post_type' => 'add_buying',
's' => $keywords,
'posts_per_page' => 5,
'taxonomy' => 'add_country',
'term' => 'drawing'
));
Change your second parameter to 's' and I think it's going to work:
$args = array(
'post_type' => 'post',
's' => $search_term,
'post_status' => 'publish'
);
$wp_query = new WP_Query($args);
And good luck
title (string) - use post title (available with Version 4.4).
$args = array(
'post_type' => 'post',
'title' => $title,
'post_status' => 'publish'
);
$wp_query = new WP_Query($args);

Resources