As you know, the following code publishes the latest content in a group in its WordPress
<?php
$my_query = new WP_Query('showposts=1&cat=172');
while ($my_query->have_posts()):
$my_query->the_post();
$do_not_duplicate = $post->ID;
?>
I want to make changes to this code، So that the last article in a group is not displayed and
, And the pre-final content of the same group will be displayed.
thank u
I think you might want to take a look at the offset and order parameters.
I'm guessing that the snapshot in your question is what you call a "group"...
Depending on what you call the last article (last published or first published) you might want to change the order from 'DESC' to 'ASC'.
<?php $query = new wp_query( array( 'post_type' => 'post', 'cat' => '172', 'post_status' => 'publish', 'posts_per_page' => '3', 'offset' => '1', 'order' => 'DESC' ) ); ?>
<?php if ( $query->have_posts() ): while ( $query->have_posts() ): $query->the_post(); ?>
<a aria-label="<?php the_title(); ?>" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php endwhile; endif; ?>
gl.
EDIT: Just an idea... You could also simply go with pure CSS and targeting your last post with :last-child ... see https://developer.mozilla.org/fr/docs/Web/CSS/:last-child
Related
In wordpress i am creating a custom loop/query throughwhich i pass certain parameters. As i click through pages however the last page duplicates some posts/products inorder to satisfy the posts_per_page variable however i would like to specify that i dont want any repetitions. Is there a standard way to do this? It would seem like a pretty obvious point.
<?php
$args = array( 'post_type' => 'product', 'posts_per_page' => 5, 'product_cat' => 'products', 'orderby' => 'rand' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
<li class="product">
<a href="<?php echo get_permalink( $loop->post->ID ) ?>" title="<?php echo esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID); ?>">
<?php woocommerce_show_product_sale_flash( $post, $product ); ?>
<?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog'); else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" width="300px" height="300px" />'; ?>
<h3><?php the_title(); ?></h3>
<span class="price"><?php echo $product->get_price_html(); ?></span>
</a>
<?php woocommerce_template_loop_add_to_cart( $loop->post, $product ); ?>
</li>
<?php endwhile; ?>
<?php previous_posts_link('« Previous', $loop ->max_num_pages);
next_posts_link(' Next »', $loop ->max_num_pages); ?>
<?php wp_reset_query();?>
It would seem like a pretty obvious point.
Not if you're using 'orderby' => 'rand' on your query, which is also very expensive by the way on large tables.
If you want to make sure that the items which already have been displayed will be excluded in the upcoming queries you'll need to save the post_ids which already has been displayed and pass them to the post__not_in parameter, see the codex page an search for post__not_in .
You could do something like this which should help you get the idea:
...
// don't forget to initialize the session somewhere
$already_displayed_post_ids = [];
if ( ! isset( $_SESSION['already_displayed_post_ids'] ) {
$_SESSION['already_displayed_post_ids'] = $already_displayed_post_ids;
} else {
$already_displayed_post_ids = array_merge( $already_displayed_post_ids, $_SESSION['already_displayed_post_ids'] );
}
$args = [
'post_type' => 'product',
'posts_per_page' => 5,
'product_cat' => 'products',
'orderby' => 'rand',
'post__not_in' => $already_displayed_post_ids
];
$loop = new WP_Query( $args );
...
I want to list latest child connected post ordered by parent post. Is it possible? Could anyone help me with that? I explain with an example: let's say I have Dramas custom post type and an Episode custom post type, that are connected to Posts 2 Posts. In the Drama archive, I want to list all the dramas by their latest episode and want to show only one episode in the loop.
I think it is maybe possible with each_connected(), but I don't know how to order the $wp_query array to get this done.
Thanks.
I am using with such kind of code now
<?php
global $post;
$paged = ( get_query_var('page') ) ? get_query_var('page') : 1;
$anime_series = array(
'post_type' => 'drama',
'posts_per_page' => 12,
'paged' => $paged,
'page' => $paged,
'post_status' => 'publish',
'orderby' => 'post_date',
'order' => 'DESC'
);
$the_query = new WP_Query( $anime_series);
$extra = array(
'posts_per_page' => 1,
'post_status' => 'publish',
'orderby' => 'post_date',
'order' => 'DESC'
);
p2p_type( 'drama_to_episode' )->each_connected( $the_query, $extra, 'episode' );
?>
</div>
<div class="last_episodes loaddub">
<ul class="items">
<?php if ( $the_query->have_posts() ) : ?>
<?php while ($the_query->have_posts()) : $the_query->the_post(); ?>
<li>
<div class="img"> <img alt="<?php echo the_title(); ?>" itemprop="photo" src="<?php echo get_the_post_thumbnail_url(); ?>">
<div class="type"><img src="<?php echo get_stylesheet_directory_uri(); ?>/images/SUB.png"></div>
</div>
<?php
foreach ( $post->episode as $post ) : setup_postdata( $post ); ?>
<p class="name"> <?php echo get_the_title($items['parent']); ?> </p>
<p class="episode"> <?php echo the_field('short_name'); ?> </p>
<?php
endforeach;
?>
</li>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p>
<?php esc_html_e( 'Sorry, no posts matched your criteria.' ); ?>
</p>
<?php endif; ?>
</ul>
</div>
Try this easy example
?php
$args = array(
'post_parent' => 38,
'post_type' => 'page',
'numberposts' => -1,
'post_status' => 'any'
);
$children = get_children( $args );
foreach($children as $post):
echo $post->post_title;
endforeach;
?>
Hope that work it in your requirement!!!
Luckily, I solved my problem myself. My primary concern was to get the latest connected child post with its parent. Before in default loop, all the episodes (old/new) were being retrieved. I made a custom select query for Posts to Posts plugin. This query returned the IDs of the latest connected post. The working code is given below. Kindly, correct me if I am using the wrong programming standards.
global $post, $items, $wpdb;
$table_name = $wpdb->prefix . "p2p";
$query = 'SELECT p2p_from as anime, max(p2p_to) as episode FROM '.$table_name;
$results = $wpdb->get_results( $query.' GROUP BY p2p_from ORDER BY episode DESC' );
Then, in foreach loop, I retreived the post title, permalink etc by the ID.
I'm hoping someone can help me with this. I'm new to php so it is very much learning on the job.
I am customising an existing Wordpress theme but I am facing a problem with the themes advanced search/results.
The theme has no way to order search results and I know that the default order for Wordpress is by date. Currently if I perform a search, the results are displayed in date order but I need the results to be price high to low.
The current code is as follows
<?php /* If there are no posts to display, such as an empty archive page */ ?>
<?php if ( ! have_posts() ) : ?>
<article id="post-0" class="post error404 not-found">
<h1 class="posttitle"><?php _e( 'Not Found', THE_LANG ); ?></h1>
<div class="entry">
<p><?php _e( 'Apologies, but no results were found for the requested property archive. Perhaps searching will help find a related post.', THE_LANG ); ?></p>
</div>
</article>
<?php endif; ?>
<div class="nvr-prop-container row">
<?php if( have_posts() ){ ?>
<div class="search-title twelve columns">
<h4><?php _e('Search Result', THE_LANG); ?> (<?php echo $wp_query->post_count; ?>)</h4>
</div>
<?php
$nvr_idnum = 0;
$nvr_typecol = "nvr-prop-col";
$nvr_imgsize = "property-image";
?>
<ul id="nvr-prop-search" class="<?php echo esc_attr( $nvr_typecol ); ?>">
<?php
while ( have_posts() ) : the_post();
$nvr_idnum++;
echo nvr_prop_get_box( $nvr_imgsize, get_the_ID(), 'element columns', $nvr_unit, $nvr_cursymbol, $nvr_curplace );
$nvr_classpf="";
endwhile; // End the loop. Whew.
?>
I then decided to try and sort the results so I created
$sort_properties = new WP_Query(array(
'post_type' => 'properties',
'meta_key' => $nvr_initial.'_price',
'meta_value' => $nvr_price,
'orderby' => 'meta_value_num date',
'order' => 'DESC',
));
<?php /* If there are no posts to display, such as an empty archive page */ ?>
<?php if ( ! have_posts() ) : ?>
<article id="post-0" class="post error404 not-found">
<h1 class="posttitle"><?php _e( 'Not Found', THE_LANG ); ?></h1>
<div class="entry">
<p><?php _e( 'Apologies, but no results were found for the requested property archive. Perhaps searching will help find a related post.', THE_LANG ); ?></p>
</div>
</article>
<?php endif; ?>
<div class="nvr-prop-container row">
<?php if( $sort_properties->have_posts() ){ ?>
<div class="search-title twelve columns">
<h4><?php _e('Search Result', THE_LANG); ?> (<?php echo $wp_query->post_count; ?>)</h4>
</div>
<?php
$nvr_idnum = 0;
$nvr_typecol = "nvr-prop-col";
$nvr_imgsize = "property-image";
?>
<ul id="nvr-prop-search" class="<?php echo esc_attr( $nvr_typecol ); ?>">
<?php
while ( $sort_properties->have_posts() ) : $sort_properties->the_post();
$nvr_idnum++;
echo nvr_prop_get_box( $nvr_imgsize, get_the_ID(), 'element columns', $nvr_unit, $nvr_cursymbol, $nvr_curplace );
$nvr_classpf="";
endwhile; // End the loop. Whew.
?>
Now when I perform a search, the posts are sorted based on price which is fantastic but... now regardless of how I search all of the site posts are now being displayed.
I felt I was so close to finding a solution but I would very much grateful if someone cold advise me with this.
Kind regards
S
Have you tried to order only by the 'meta_val_num' without the 'date' like:
$args = array(
'post_type' => 'product',
'orderby' => 'meta_value_num',
'meta_key' => 'price',
);
$query = new WP_Query( $args );
Code from: https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters
I think you have to add the Search Parameter and probably use the new meta_query structure:
$search_query = get_search_query();
$sort_properties = new WP_Query(array(
'post_type' => 'properties',
's' => $search_query,
'orderby' => 'meta_value_num',
'meta_query' => array(
'key' => $nvr_initial.'_price',
'value' => $nvr_price,
)
));
I remember having some trouble, not using "meta_query" => array() for multiple conditions.
Here is the very weird issue I'm dealing with at the moment: I am trying to implement a lightbox effect in my wordpress theme. Following this tutorial here (the CSS code used is this one: link). I tried it as it is, it works very well.
But I'm not looking for applying the effect to the featured image but to all images in a post. I have changed the code to match my need, it works if I have only one picture in the post but if I add another one (or more) that's when the problem starts:
when I click on a picture, no matter which one, it's only the first one that get displayed in the large version. But when I check the source code, the relevant picture is called for the large version of each picture.
Here's the bit of code I'am using for this lightbox effect:
<?php
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => null,
'post_parent' => $post->ID,
'post_mime_type'=> 'image',
'orderby' => 'ID',
'order' => 'ASC',
);
$attachments = get_posts( $args );
?>
<div class="nine columns">
<div class="about">
<h2><?php the_title(); ?></h2>
<?php foreach ( $attachments as $attachment ) :
$attachments = get_posts( $args );
$small_image = wp_get_attachment_image_src($attachment->ID, 'thumbnail');
$large_image = wp_get_attachment_image_src($attachment->ID, 'full');
?>
<img src="<?php echo $small_image[0]; >" alt="small">
<img src="<?php echo $large_image[0]; ?>" alt="large">
<?php endforeach ?>
</div>
</div>
Thanks in advance for looking into my request
With kind regards
Wanderer
Your code has three problems:
1. should not call $attachments = get_posts( $args ); in foreach
2. the href of the small image link is incorrect
3. the id of large image link is incorrect
Try this:
<?php
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => null,
'post_parent' => $post->ID,
'post_mime_type'=> 'image',
'orderby' => 'ID',
'order' => 'ASC',
);
$attachments = get_posts( $args );
?>
<div class="nine columns">
<div class="about">
<h2><?php the_title(); ?></h2>
<?php foreach ( $attachments as $attachment ) :
$small_image = wp_get_attachment_image_src($attachment->ID, 'thumbnail');
$large_image = wp_get_attachment_image_src($attachment->ID, 'full');
?>
<img src="<?php echo $small_image[0]; ?>" alt="small">
<img src="<?php echo $large_image[0]; ?>" alt="large">
<?php endforeach ?>
</div>
</div>
Thing is, In the homepage of my theme, I want to show post from different category in Different Div. Each DIV will contain 3 post from a category. I need a loop that can pick last 3 post from a specific Category. Can't find any suitable ans for it.
To explain things more easily, here is a demo picture of the Content section,
http://i.imgur.com/5QSzAIS.png
It will be a great help, if someone help me with the code !
<?php query_posts('cat=10&posts_per_page=3'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; endif; ?>
This should get you started. You need to use this code twice. Where it says cat=10, you should enter your category ID (you can check this when you click on a Category from the admin panel, the the browser it will show something like this http://yourwebsite.com/wp-admin/edit-tags.php?action=edit&taxonomy=category&tag_ID=4&post_type=post)
Where it says tag_ID is the ID of your category.
I'm currently using a different method on a page of the site I'm building which allows me to run multiple loops in one page and specify the category for each one. This method I personally like better as it is more straightforward to me, and I can define the category with the slug instead of the ID.
Instead of using have_posts() and such, you use WP_Query() after defining your array and then wp_reset_postdata() to end your loop. The benefit is that you can keep running loops this way.
I'm also loading the data from custom fields in my posts using get_post_meta, but this method will work without that stuff.
<div class="audioGrid">
<?php
$args = array( 'post_type' => 'post',
'category_name' => 'audio',
'posts_per_page' => 3,
'order' => 'DESC' );
$query1 = new WP_Query($args);
while ( $query1->have_posts() ) {
$query1->the_post();
?>
<div id="<?php echo( basename(get_permalink()) ); ?>" class="grid_item">
<?php the_post_thumbnail( 'audio-thumb' ); ?>
<h3><?php the_title(); ?></h3>
<p><?php echo get_post_meta($post->ID, 'post_description', true); ?></p>
<a target="blank" href="<?php echo get_post_meta($post->ID, 'audio_link', true); ?>"></a>
</div>
<?php the_content(); ?>
<?php } ?>
</div> <?php // end Audio Grid ?>
<?php wp_reset_postdata(); ?>
<div class="videoGrid">
<?php
$args2 = array( 'post_type' => 'post',
'category_name' => 'video',
'posts_per_page' => 3,
'order' => 'DESC' );
$query2 = new WP_Query($args2);
while ( $query2->have_posts() ) {
$query2->the_post();
?>
<div id="<?php echo( basename(get_permalink()) ); ?>" class="grid_item">
<?php the_post_thumbnail( 'video-thumb' ); ?>
<h3><?php the_title(); ?></h3>
<p><?php echo get_post_meta($post->ID, 'post_description', true); ?></p>
<a target="blank" href="<?php echo get_post_meta($post->ID, 'video_link', true); ?>"></a>
</div>
<?php the_content(); ?>
<?php } ?>
</div> <?php // end Video Grid ?>
<?php wp_reset_postdata(); ?>
Another cool thing I'm doing is using a custom field to define the order of things and using meta_key and meta_value_num to get that number and force the order how I want, and since this site isn't complicated, defining the order this way is convenient. I just use leading zeroes to make it easy: 001, 002, 003, etc
<?php
$args2 = array( 'post_type' => 'post',
'category_name' => 'video',
'posts_per_page' => 3,
'meta_key' => 'video_order',
'orderby' => 'meta_value_num',
'order' => 'ASC' );
$query2 = new WP_Query($args2);
while ( $query2->have_posts() ) {
$query2->the_post();
?>
Anyway, hope this helps if you need to use multiple loops to pull posts from different categories.