I was working on my personal blog theme, and I want to list all my "image post" (have image(s) in it) to a page, so I built a page template, but don't know how to make WordPress query only those posts with image(s).
I know how to query posts with featured image but my blog is too old and lots of posts don't have a featured image. So if I can search and query post with "
Is there a way to make a custom query to get all posts with
And I find the solution now, a simple search for "img" tag will perfectly do the job, the query goes like this
<?php
// set up or arguments for our custom query
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$query_args = array(
'post_type' => 'post',
's' => '<img',
'posts_per_page' => 9,
'paged' => $paged
);
// create a new instance of WP_Query
$the_query = new WP_Query( $query_args );
if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); // run the loop
?>
Related
Wrote this code to display specific category from custom post type on wp page.
<?php
$paged = (get_query_var('paged')) ?get_query_var('paged') : ((get_query_var('page')) ? get_query_var('page') : 1);
$blog_items_num = ($data['blog_item_number']) ? $data['blog_item_number'] : 3;
$blog_order = ($data['blog_order']) ? $data['blog_order'] : "date";
$args = array(
'post_type' => 'aeolus_news',
'cat'=>'24',
'posts_per_page'=>$blog_items_num,
'orderby'=>$blog_order,
'paged' => $paged
);
query_posts($args);
rewind_posts();
get_template_part( 'content-news', 'single' );
wp_reset_query();
?>
But nothing displays.
If comment out 'cat' => '24' then all posts/categories from the custom post type display.
Suggestions?
First, consider using WP_Query() instead of query_posts for post queries like this (see this WPSE answer for details why)
Second of all, if you're using the category id, it's expecting an integer not a string, and the apostrophes around the id are making it a string.
Replace 'cat' => '24', with 'cat' => 24, (with no quotation marks around the 24). Right now it's looking for a category literally named "24".
I am struggling with the pagination of a custom loop in WordPress …
The Links are displayer correctly, but when I click on the next page I get a 404 Error.
The Link seems to be correct http://…/page/2/
Did I miss something while creating the query?
Do I have to add an additional function in the functions.php file?
This is my query:
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$wu_media_query = new WP_Query( );
$wu_media_query->query('showposts=5&post_type=wu_media&paged=' . $paged);
Would be grateful any hint :-)
Dominik
First, go to Settings >> Permalinks and hit Save Changes button. Sometime you need to regenerate you .htaccess file to make the pagination work.
Make sure you've called wp_reset_query() after ending the while loop or after the default WordPress loop. Sometimes it make this kinda trouble.
Pass 'paged' => $paged with the query arguments array to the WP_Query
$paged = ( get_query_var('page') ) ? get_query_var('page') : 1;
$query_args = array(
'post_type' => 'post',
'category_name' => 'tutorials',
'posts_per_page' => 5,
'paged' => $paged // Like this.
);
Remember, for custom pages that are NOT static home pages, the $paged variable changes to this:
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
Hope the above helps you.
Is there any way for change all custom posts type status change from "publish" to Draft ?
Scenario:
Here I need to run a script.
my post type is: property_listing
Now I need to make all post draft then perform my custom operation.
Is it possible ?
If you have Cpanel or database access you can run this query in DB panel.
update table wp_posts set status='draft' where post_type='property_listing'
otherwise, use this code.( paste it in your theme function.php)
add_action('init','change_mypost_status');
function change_mypost_status(){
$args = array(
'post_type' => 'property_listing',
'posts_per_page' => -1
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
$current_post = array(
'ID' => get_the_ID(),
'post_status' => 'draft'
);
// Update the post into the database
wp_update_post( $current_post );
}
wp_reset_postdata();
}
}
I want to fetch the woocommerce product short description using the wpdb query can anyone tell me how to do so?
You can use the_excerpt to get the Short Description.
$loop = new WP_Query( array( 'post_type' => 'product','post_status' => 'publish', 'posts_per_page' => '-1' ) );
while ( $loop->have_posts() ) : $loop->the_post();
the_excerpt();
endwhile;
So it will give you the Short Description if product have a short description.If not, then it will show you post content.
I'm struggling a theme that I got, the theme uses query_posts() and pagination used to work. Now pagination is not working on this page, and it keeps on showing posts from the first page on the second. Meaning, the url shows page/2 but I keep on seeing the first posts of the category, i.e. the first 4 on every page.
Here is the code used to get the posts:
global $current_category;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
global $query_string;
$args = "";
if ($current_category['post_type'] == "product") {
$args=array(
'showposts' => 4,
'paged' => $paged,
'post_type' => 'product'
);
} else {
$args=array(
'showposts' => 4,
'category_name' => $current_category['name'],
'paged' => $paged
);
}
query_posts($args);
And here's the loop:
if (have_posts()) : while (have_posts()) : the_post();
<outputs code here>
endwhile;
else :
<output no results code here>
endif;
if ( is_home() ) wp_reset_query();
Now, can anybody please point me in the right direction?
//update:
I have already tried this solution as well, so far, I can only tell that the paged variable never gets updated in the query.
//update 2:
This page was done custom, and setting it as the home page via Settings -> Reading inhibits the above behaviour. When leaving it as a normal page, and setting home as recent posts, the pagination works fine.
if you have any custom queries in the template, make sure to integrate the 'paged' parameter.
Pagination Parameters
Go through these codex pages:
Preserving Existing Query Parameters
Related: Pagination
I have fixed this after some very long research. My code now looks as follows:
global $current_category, $paged;
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} elseif ( get_query_var('page') ) {
$paged = get_query_var('page');
} else {
$paged = 1;
}
This just replaces the first 3 lines of the original code in the answer. The rest looks exactly the same.