I'm using WordPress with a template that generates a pretty nice thumbnail for each post depending on Ids, and type of post. (ref:https://blinkdemo.wordpress.com/)
Since I've been asked to create a custom page that could show certain post from a category, I decided to create a query for a template page that checks the page slug and then list the posts containing a certain category + a tag ('comparativas').
The problem that I'm facing is that the list of post presented on the page doesn't show up the corresponding thumbnail on each post.
Thumbnails are generated dynamically basically with these lines:
$post_id = $post->ID;
$thumbnail_id = get_post_thumbnail_id( $post_id );
$thumbnail_image = wp_get_attachment_image_src( $thumbnail_id,$thumbnail_size );
The problem is that I couldn't find the way to send to the specific post ids to the function above, since the main $wp_query->posts; retrieves the page id instead of the post requested by the query_posts method.
The loops present the correct posts but when I echo the post->ID it shows the page id.
My query is:
global $wp_query;
// concatenate the query
$args = 'cat='.$idCategory.'&tag=comparativas';
query_posts( $args );
$posts = $wp_query->posts;
$current_id = get_the_ID(); //-> this returns the page id
If you could please tell me how to overwrite the global $wp_query; so the template can handle the corresponding ids for the list of post It would be great.
Any clue?
Best,
Juan
You could use, setup_postdata($post)
Then get_the_ID() works again :)
It doesn't work just because you aren't looping through them. You can do it many ways.
The 2 more common are the following:
overwrite the query with query_posts
$args = array('cat' => $idCategory,'tag' => 'comparativas');
query_posts($args);
if(have_posts()){
while(have_posts()){
the_post();
$current_id = get_the_ID(); // this return what you want now
the_title(); // this works as expected
}
}
wp_reset_query(); // get previous query back
get an array of WP_Post objects and setup_postdata or simply loop through them
$args = array('cat' => $idCategory,'tag' => 'comparativas');
$posts_i_want = get_posts($args);
foreach( $posts_i_want as $post ){
setup_postdata($post);
$current_id = get_the_ID(); // this return what you want now
the_title(); // this works as expected
}
wp_reset_postdata(); // get previous postdata back
I personally prefer the first in most cases
Cheers
Related
I have used CPT UI to add some posts with taxonomies. I have filled two post data in CPT UI for practice. Now I want to show these post on a page. What all code I have to write.
You can use Wp_Query along with the post name that you created using CPT Ui plugin to display those posts. Like, e.g. i had created a post named as school then code to display all posts of School type is as following :
$query = new WP_Query( array( 'post_type' => 'school' ) );
while($query->have_posts()):
$query->the_post();
echo $query->ID; // it will print the ID of post
endwhile;
Hope this will clear the things..
In order to pull in the custom fields/post meta, you will need to write some code within the WordPress loop (https://codex.wordpress.org/The_Loop) in your template file.
The Loop is PHP code used by WordPress to display posts. Using The Loop, WordPress processes each post to be displayed on the current page, and formats it according to how it matches specified criteria within The Loop tags. Any HTML or PHP code in the Loop will be processed on each post.
eg.
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
//
// Post Content here
//
} // end while
} // end if
For all post meta:
$meta = get_post_meta( get_the_ID() );
echo '<pre>';
print_r( $meta );
echo '</pre>';
Or for a single value:
$custom_field_value = get_post_meta( get_the_ID(), 'custom_field_key_name', true );
See below for more information in the WordPress Codex:
https://codex.wordpress.org/Custom_Fields
and
https://developer.wordpress.org/reference/functions/get_post_meta/
I am trying to get custom posts with WP_Query but it's not returning only custom post type posts but also default posts too.
I am using
$args = array (
'post_type' => array ('survey')
);
$sPosts = new WP_Query($args);
as a result I am getting 'survey' posts as well as default posts, I only need it to return 'survey' posts.
P.S. when I use query_posts() it returns the required result, but just not getting it done with WP_Query and I prefer to use WP_Query not the query_posts()
Please try it like that....
<?php
$new = new WP_Query('post_type=discography');
while ($new->have_posts()) : $new->the_post();
the_content();
endwhile;
?>
I believe this custom query should actually be your main query, in which case you should not use a custom query
The problem you are facing is either due to
Wrong use somewhere of pre_get_posts. Remember, pre_get_posts alters all instances of WP_Query, backend and front end. Lack of correct use will break all queries
You have not changed the loop to be objects of your new query
To come back to the point, as said, this is suppose to be the main query, so lets target that problem.
The first thing to do would be to delete the custom query. Once you have done this, you would only see normal post.
We are now going to use pre_get_posts to alter the main query to only show custom posts. Paste the following inside your functions.php
add_action( 'pre_get_posts', function ( $q ) {
if( !is_admin() && $q->is_main_query() && $q->is_home() ) {
$q->set( 'post_type', 'YOUR CPT' );
}
});
You should now just see post from your cpt on the homepage
EDIT
Your index.php should look something like this
if( have_posts() ) {
while( have_posts() ) {
the_post();
// HTML mark up and template tags like the_title()
}
}else{
echo 'No posts found';
}
This code can get all the posts in a custom post type..,
wp_query is the function can get all the posts in the custom post type. wp_query requires array, so you can give the custom post type name in post id to array
$args = array(
'post_type' => 'address', //custom post type
'posts_per_page' => -1 // get all posts
);
$query = new WP_Query( $args );
// Check that we have query results.
if ( $query->have_posts() ) {
// Start looping over the query results.
while ( $query->have_posts() ) {
$query->the_post();
echo( get_the_title() );
}
}
I don't know if this is possible but I'll try to explain.
I have a custom post type (creatives) and a taxonomy (image-sort). Every post in the CPT is a person (a creative). They can upload images and sort them by choosing which categories (the 'image-sort' taxonomy) they belong to.
There are multiple creatives, and they will post images to different categories. Some to all of them, some to just a few.
Every creative have their own page with a dynamic listing of which categories they have posted content to. My problem is though that if one creative have posted to 'cat-1' and 'cat-2' everybody get that listed out on their respective page. What I want is to only show 'cat-1' and 'cat-2' on the creative which has posted to those categories. If another creative has posted to 'cat-1' and 'cat-3' I only want those two to appear on his page.
Does this make sense?
<ul>
<?php
$terms = get_terms('image-sort');
if ( $terms && !is_wp_error($terms) ) :
foreach ( $terms as $term ) :
?>
<li><?php esc_html_e($term->name); ?</li>
<?php endforeach; endif; ?>
</ul>
Not sure if i understand you, but if i do, try replacing this line:
$terms = get_terms('image-sort');
with the following line:
$terms = wp_get_object_terms(get_the_ID(), 'image-sort');
EDIT
Try fetching the terms with the following piece of code:
<?php
// get all attachments of the current post
$attachments = get_children('post_parent=' . get_the_ID() . '&post_type=attachment&post_status=any&posts_per_page=-1');
// we're saving the terms here
$all_terms = array();
// looping through all attachments
foreach( $attachments as $attachment) {
$terms = wp_get_object_terms($attachment->ID, 'image-sort');
if ($terms) {
// looping through all attachment terms and adding them to the main array
foreach ( $terms as $term ) {
$all_terms[$term->term_id] = $term;
}
}
}
?>
I have made a shortcode inside my plugin, which is working great .
The shortcode needs to take some parameters and create a custom loop with output.
One of the parameters is how many posts to output the loop for ($markers)
$args=array(
'meta_key'=>'_mykey',
'post_status'=>'publish',
'post_type'=>'post',
'orderby'=>'date',
'order'=>'DESC',
'posts_per_page'=>$markers,
);
$wp_query = new WP_Query();
$wp_query->query($args);
if ($wp_query->have_posts()) : while (($wp_query->have_posts()) ) : $wp_query->the_post();
// do the loop using get_the_id() and $post->id
endwhile;endif;
wp_reset_query();//END query
On occations I will need to have data from ALL posts ($markers = '-1' ) and sometimes only one ($markers = '1' ) or muliple ($markers = 'x').
All of those work great on single pages / posts - but My problem is that when this function is in a place where I have more than one post (!is_single) and ($ markers = '1' )it will always return the data for the LATEST post , and not for the correct one ..
(for example in the default wordpress theme, where it would display10 posts - they will all be the same data )
It is obviously a problem of the $post->ID - but how can I have the correct post ID when doing a custom loop OUTSIDE the wp loop ?
I tried to ovverride the problem by
global $post;
$thePostIDtmp = $post->ID; //get the ID before starting new query as temp id
$wp_query = new WP_Query();
$wp_query->query($args);
// Start Custom Loop
if (!is_single()){
$post_id_t = $thePostIDtmp;}
else {
$post_id_t = $post->ID;}
and then use $post_id_t - but it did not seems to work ,
Should I not use get_the_id() ? or should I not use query (and use get_posts) ??
Any ideas / solutions / thoughts ??
I would use query_posts(http://codex.wordpress.org/Function_Reference/query_posts)rather than override the $wp object. You should be able to include as many loops on the page as you want with this. If you have problems with this you can use: http://codex.wordpress.org/Function_Reference/wp_reset_query just before you call it.
I find this: http://blog.cloudfour.com/wordpress-taking-the-hack-out-of-multiple-custom-loops/
takes a bit of the pain away too.
There are basically two sorts of querying posts in WordPress: Those that alter the main loop and those that do not. If you want to change the main loop like the one used to display category archive pages then use query_posts. It let's you do exactly that. Delete, change and append parameters of the default query to change the outcome of a typical page.
query_posts has some drawbacks though.
Then there are queries that are just used to get stuff out of the database to play around with e.g. displaying the latest post titles in the sidebar or the attachments of the current post.
To do that create a new WP_Query object that will build your custom loop independently of the main loop like so:
// The Query
$the_query = new WP_Query( $args );
// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;
// Reset Post Data
wp_reset_postdata();
Then there is get_posts() which is like the little brother of WP_Query. It has an easier interface in my opinion and returns an array with the results that is easier to work with.
It looks like this:
$myposts = get_posts( $args );
foreach($myposts as $post) : setup_postdata($post);
echo "<li>";
the_title();
echo "</li>";
endforeach;
Inside the foreach template tags like get_the_id() will work.
Is there a function in wordpress to iterate through all posts and discard posts with same name? E.g. I have several posts named "Banana" and I need just one post with that name to be shown?
It's actually custom post type and I am using WP_Query to query posts ?
Thanks,
Peter
I don't believe there is such a function. But you could easily add this functionality to a vanilla WP_Query loop. Here's one solution that uses an array to "remember" post names during iteration:
<?php
// The Query
$the_query = new WP_Query( $args );
// an array to remember titles
$titles = array()
// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post();
$the_title = get_the_title(); // (1) grab the current post title
if ($titles[$the_title]) continue; // (2) duplicate title: skip post!
$titles[$the_title] = TRUE; // (3) otherwise, remember the title
// ..do post stuff..
endwhile;
// Reset Post Data
wp_reset_postdata();
?>
The lookup for (2) is fast because Array is implemented as a map in PHP. See Arrays for more info.