Not sure if it’s a support question, but I hope to find some solution here:
I have a page with multiple “latest posts” widgets on it, showing 3 latests post from each category. And I will have like 10-12 categories like that. I’m not sure if this page will overload a server/db, so asking if there is any recommended settings for pages like this?
Current cache report from that page:
Object cache 129/411 with memcached
Page cache – disk: enhanced
DB cache 6/60 queries in 0.114 sec with redis
Thanks!
]]>This is the loop below, nearly identical to they way WP suggest to use multiple loops one one page:
<?php $my_query = new WP_Query( array(
'category__and' => array(23,2)
));
while ( $my_query->have_posts() ) : $my_query->the_post();
$do_not_duplicate[] = $post->ID; ?>
<?php get_template_part( 'parts/loop', 'archive-grid' ); ?>
<?php endwhile; ?>
<?php query_posts( array(
'post__not_in' => $do_not_duplicate,
'category__in' => array(2)
));
if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'parts/loop', 'archive-grid' ); ?>
<?php endwhile; endif; ?>
Ajax Load More – Infinite Scroll works great. I’ve already recommended it to a few people, i just need to get past this one issue.
]]>This is the code I’m using :
<?php $presentevents = eo_get_events(array(
'events_start_before'=>'today',
'events_end_after'=>'today',
'showpastevents'=>false,//Will be deprecated, but set it to true to play it safe.
));
if( $presentevents ){
echo '<h2>', 'Eventos en curso', '</h2>';
foreach( $presentevents as $event1 ){
printf("<article><a href='%s' >%s</a> from %s to %s </article>",
get_the_permalink($event1->ID),
get_the_title($event1->ID),
eo_get_the_start('jS F Y', $event1->ID,null,$event1->occurrence_id),
eo_get_the_end('jS F Y', $event1->ID,null,$event1->occurrence_id)
);
}
}else{
echo 'No hay eventos en curso';
}
wp_reset_postdata();
?>
<?php $futurevents = eo_get_events(array(
'events_start_after'=>'tomorrow',
'showpastevents'=>false,//Will be deprecated, but set it to true to play it safe.
));
if( $futurevents ){
echo '<h2>', 'Eventos futuros', '</h2>';
foreach( $futurevents as $event2 ){
printf("<article><a href='%s' >%s</a> from %s to %s </article>",
get_the_permalink($event2->ID),
get_the_title($event2->ID),
eo_get_the_start('jS F Y', $event2->ID,null,$event2->occurrence_id),
eo_get_the_end('jS F Y', $event2->ID,null,$event2->occurrence_id)
);
}
}else{
echo 'No hay eventos futuros';
}
wp_reset_postdata();
?>
https://www.ads-software.com/plugins/event-organiser/
]]>Searching the Internet I found most people use query_posts to achieve that, but I read in the Codex that it would be better to use pre_get_post instead. In fact, the first 2 examples in the pre_get_post page of the codex sounded like what I was looking for, but they don’t work if you have 2 loops.
So I searched for multiple loops, and again I found what I was looking for in the Codex, but, alas, those examples were using the dreaded query_posts.
So my questions is: Can I safely use query_posts, or is there a better way?
]]><div id="headline">
<?php
/* Headline Query */
$query2 = new WP_Query( 'post_type=headline' );
while ( $query2->have_posts() ) {
$query2->the_post();
get_template_part( 'template-parts/content', get_post_format() );
}
// Restore original Post Data
wp_reset_postdata();
?>
</div>
</main><!-- END #main -->
<div id="subpages">
<?php if ( have_posts() ) : ?>
<?php if ( is_home() && ! is_front_page() ) : ?>
<header>
<h1 class="page-title screen-reader-text"><?php single_post_title(); ?></h1>
</header>
<?php endif; ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php
/*
* Include the Post-Format-specific template for the content.
* If you want to override this in a child theme, then include a file
* called content-___.php (where ___ is the Post Format name) and that will be used instead.
*/
get_template_part( 'template-parts/content', get_post_format() );
?>
<?php endwhile; ?>
<?php the_posts_navigation(); ?>
<?php else : ?>
<?php get_template_part( 'template-parts/content', 'none' ); ?>
<?php endif; ?>
</div><!-- #subpages -->
]]>post_ID
s in the widget code in order to exclude these posts from the main loop.
So on the front page (= blog page) the theme displays the following:
Each post can have more than one category.
The main loop is supposed to exclude those posts that already have been queried by WIDGET 1 and WIDGET 2.
So in the WP Codex there is a paragraph about how to do this with multiple loops. The solution looks like this:
For the custom loop
<?php $my_query = new WP_Query( 'category_name=featured&posts_per_page=2' );
while ( $my_query->have_posts() ) : $my_query->the_post();
$do_not_duplicate[] = $post->ID; ?>
For the main loop:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();
if ( in_array( $post->ID, $do_not_duplicate ) ) continue; ?>
**Idea**
As you can see, I use the same widget more than once. This makes me think I can’t just go and use $do_not_duplicate[] = $post->ID;
in my widget code because it’ll get overwritten, won’t it?
How can I store the IDs of the posts that have already been queried by the widgets in a variable or an array to use them in my index.php
file ?
I’m glad if anyone could give his/her thoughts on this.
]]>I’ve been trying to modify my loop. On category and tag pages I’d like to have to posts listed the following:
–> first show all posts for this category that are NOT also listed in category X
–> then show all posts for this category that are also listed in category X
I did some reading and managed to achieve that with query_posts
, but this causes problems with the pagination … (and apparently it’s a bad idea to use this method anyway)
I tried WP_Query
but then it shows all posts, not just the ones for this category.
Can anyone help me out?
Thanks in advance
Apologies if this is a repost but I couldn’t find anything that really covered what I’m trying to do. I’ll try to summarise my problem briefly…
I’m trying to style the first post on my front page loop differently from the rest. I know that’s not an uncommon request, but it has to apply to only the first post of the first page.
Not only have I been unable to get that working, I also haven’t figured out how to apply Foundation classes to it, so the first post is eg. large-12 columns and the rest are either large-6 columns or part of a block grid.
My current loop is as follows…
<?php
$paged = ( get_query_var('page') ) ? get_query_var('page') : 1;
$query_args = array(
'post_type' => array ('post' , 'reviews'),
'posts_per_page' => 10,
'paged' => $paged
);
$the_query = new WP_Query( $query_args );
?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div>
<h1><?php echo the_title(); ?></h1>
<div class="excerpt">
<?php the_excerpt(); ?>
</div>
</div>
<?php endwhile; ?>
<?php if ($the_query->max_num_pages > 1) { ?>
<nav class="prev-next-posts">
<div class="prev-posts-link">
<?php echo get_next_posts_link( 'Older Entries', $the_query->max_num_pages ); ?>
</div>
<div class="next-posts-link">
<?php echo get_previous_posts_link( 'Newer Entries' ); ?>
</div>
</nav>
<?php } ?>
<?php else: ?>
<article>
<h1>Sorry...</h1>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
</article>
<?php endif; ?>
That should also be including multiple custom post types and pagination.
I’m pretty lost here and would greatly appreciate any suggestions. I’m no php expert by any means so any help would be fantastic. I also considered two loops: one to pull in the first post, another to pull in the rest. Not sure if that’s a better solution?
Thanks.
]]>I’m trying to make an attachment template where the attachment image is displayed with its Caption and description fields. Then the capture line is used to call a page with the same name. Here is the code that isn’t working:
<?php
$title = get_the_title();
$title = strtolower($title);
$page = 'pagename='.$title;
?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', 'attachment' ); ?>
<?php endwhile; // end of the loop. ?>
<?php
// the query for the page with the same name as the caption
$new_query = new WP_Query( $page ); ?>
<?php if ( $new_query->have_posts() ) : ?>
<?php get_template_part( 'content', 'page' ); ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
Where am I going wrong? Thanks
]]>