How to loop within a loop in WordPress? - wordpress

How can I run a loop within a loop in WordPress?
My attempt is this so far but it seems to crash the server each time.
<?php $loop = new WP_Query( array('post_type' => 'careers', 'posts_per_page' => '10', 'meta_key' => 'company', 'meta_value' => get_field('company') )); ?>
<?php $loop2 = new WP_Query( array('post_type' => 'jobs', 'posts_per_page' => '-1', 'meta_key' => 'company', 'meta_value' => get_field('company') ));?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php $i = 0; while ( $loop2->have_posts() ) : $loop->the_post(); $i++; ?>
<?php echo $i ?>
<?php endwhile; wp_reset_query(); update_field('jobscounter', $i); ?>
<?php endwhile; wp_reset_query(); ?>

In PHP, you can open a code section by <?php and close it later with ?>. With that in mind, you can place all your code in multiple lines without opening and closing the <?php ?> block over and over.
Example:
<?php
$loop = new WP_Query(array(
'post_type' => 'careers',
'posts_per_page' => '10',
'meta_key' => 'company',
'meta_value' => get_field('company')
));
$loop2 = new WP_Query(array(
'post_type' => 'jobs',
'posts_per_page' => '-1',
'meta_key' => 'company',
'meta_value' => get_field('company')
));
while ($loop->have_posts()):
$loop->the_post();
$i = 0;
while ($loop2->have_posts()):
$loop2->the_post();
$i++;
echo $i;
endwhile;
wp_reset_query();
update_field('jobscounter', $i);
endwhile;
wp_reset_query();
?>
Note: you where missing a semicolon ; after the echo $i
Note 2: the second loop should use $loop2-> instead of $loop->
Then, you should do something with the data you process in each iteration. If you don't want to just count the total entries. :)
For example:
while ($loop2->have_posts()):
$loop2->the_post();
$i++;
$post_title = $loop2->the_title(); // <--- Getting the title :)
echo $post_title . "\n";
endwhile;

Related

WP loop filtering with ACF values

How to filter by ACF fields in WP loop?
<?php
$company = the_field('company');
$loop = new WP_Query( array(
'post_type' => 'jobs',
'posts_per_page' => '100',
'meta_key' => 'company',
'meta_value' => the_field('company')
)
);
?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php get_template_part( 'components/loop' );?>
<?php endwhile; wp_reset_query(); ?>
If the meta_value is a string it works. Wondering how to make this work dynamically.
You need to use get_field instead of the_field. You can print custom-filed values with the get_field function.
$loop = new WP_Query(
array(
'post_type' => 'jobs',
'posts_per_page' => '100',
'meta_key' => 'company',
'meta_value' => get_field('company'),
)
);
?>
<?php while ($loop->have_posts()): $loop->the_post();?>
<?php get_template_part('components/loop');?>
<?php endwhile;
wp_reset_query();?>

Wordpress Custom post page with wp_pagenavi doesn't work

I have custom page for posts which has to show up 9 posts per page and I used wp_pagenavi plugin but it doesn't work. Please help!
<div class="page-content__wrapper">
<?php
$post_category = get_field('page_category');
$posts = get_posts( array(
'numberposts' => 9,
'category_name' => $post_category,
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'post',
'suppress_filters' => true,
) );
foreach( $posts as $post ){
setup_postdata($post);
?>
//...posts
<?php
}
wp_pagenavi();
wp_reset_postdata();
?>
</div>
I used WP_Query() to solve this problem (without wp_pagenavy plugin :)
<?php
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1; // setup
$post_category = get_field('page_category'); // getting category name
$posts= new WP_Query(array(
'post_type'=> 'post',
'posts_per_page' => 9,
'paged' => $paged, // don't forget to give this argument
'category_name' => $post_category,
));
if($posts->have_posts()) :
while($posts->have_posts()) : $posts->the_post(); ?>
// All posts will be here!
<?php endwhile; ?>
<div class="mt-30 text-center">
<?php
$GLOBALS['wp_query'] = $posts;
the_posts_pagination(
array(
'mid_size' => '2',
'prev_text' => '<i class="fa fa-hand-o-left"></i> <',
'next_text' => '> <i class="fa fa-hand-o-right"></i>',
'screen_reader_text' => ' '));
?>
</div>
<?php else :?>
<h3><?php _e('404 Error: Not Found'); ?></h3>
<?php endif; ?>
<?php wp_reset_postdata();?>

Get all Posts If has same custom field values in Posts

Trying to get all the posts having the same zipcode as metavalue. Thanks in advance for the help.
<?php
$query = new WP_Query( array(
'post_type'=> array('service'),
'posts_per_page' => -1,
'meta_query' => array( array(
'key'=> 'zipcode',
'value'=> ','.$zip.',',
'compare'=> 'LIKE'
) )
));
?>
<?php if ( $query->have_posts() ) :while ( $query->have_posts() ) : $query->the_post(); ?>
<h3><?php the_title(); ?></h3>
<?php endwhile; // end of the loop. ?>
<?php wp_reset_query(); ?>
<?php else: ?>
No results found.
<?php endif; ?>
zipcode are numbers for example 12345. If posts have value 12345 in the custom field. then it should display all posts which have the 12345 value. The above code is working fine but displays only one post.
Following code will be the proper for the meta query.
$query_args = array(
'post_type' => 'service',
'posts_per_page' => -1,
'meta_query' => array(
array(
'value' => $zip,
'compare' => 'LIKE',
'key' => 'zipcode',
),
)
);
$query = new WP_Query($query_args);
<?php if ( $query->have_posts() ) :while ( $query->have_posts() ) : $query->the_post(); ?>
<h3><?php the_title(); ?></h3>
<?php endwhile; // end of the loop. ?>
<?php wp_reset_query(); ?>
<?php else: ?>
No results found.
<?php endif; ?>
Hope it helps.
This does the job for me:
$popularCourses = new WP_Query(
array(
'post_type' => 'courses',
'posts_per_page' => -1,
'post_status' => 'publish',
'meta_key' => 'course-is-promoted',
'meta_value' => 'Yes',
'orderby' => 'date',
'order' => 'DESC'
)
);
There are two ways.
After watching this code i will suggest you just visit this link for your better understanding.
(1)
$args = array(
'meta_query' => array(
array(
'key' => 'Your_key',//Enter your meta key here
'value' => 'professionnel',//Enter you meta value
'compare' => '=',//Comparison type (option filed) .
)
)
);
$query = new WP_Query($args);
(2)
$output_loop = get_posts( array(
'meta_key' => 'Your_key',//Meta key
'meta_value' => 'Your_value',//Meta value
) );
Now just print_r($output_loop) for the better understanding.

Pagination not working with query_posts

I trying to add pagination to my WordPress website, it's working fine with blog page, but with custom category when click any link of pagination URL change, but content still the same.
Custom category code
<?php query_posts('category_name=sport');
if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php get_template_part('loop'); ?>
<?php endwhile; // конец цикла
else: echo '<p>Blank.</p>'; endif; ?>
<?php pagination(); ?>
Pagintion in function.php
function pagination() {
global $wp_query;
$big = 999999999;
$links = paginate_links(array(
'base' => str_replace($big,'%#%',esc_url(get_pagenum_link($big))),
'format' => '?paged=%#%',
'current' => max(1, get_query_var('paged')),
'type' => 'array',
'prev_text' => 'Назад',
'next_text' => 'Вперед',
'total' => $wp_query->max_num_pages,
'show_all' => false,
'end_size' => 4,
'mid_size' => 4,
'add_args' => false,
'add_fragment' => '',
'before_page_number' => '',
'after_page_number' => ''
));
if( is_array( $links ) ) {
echo '<ul class="pagination">';
foreach ( $links as $link ) {
if ( strpos( $link, 'current' ) !== false ) echo "<li class='active'>$link</li>";
else echo "<li>$link</li>";
}
echo '</ul>';
}
}
Please help
instead of: query_posts('category_name=sport');
try this:
$page = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'category_name' => 'sport',
'posts_per_page' => 6,
'paged' => $page
);
query_posts($args);
For further customization check the List of Query Vars
Try this:
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'posts_per_page' => 3,
'paged' => $paged,
'category_name' => 'sport'
);
$the_query = new WP_Query( $args );
?>
Hope this helps :)
Please check below code:
$page = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'work',
'categories'=>'sports',
'posts_per_page' => 2,
'paged' => $page,
);
query_posts($args);
if (have_posts()) : while (have_posts()) : the_post(); ?>
<h3><?php the_title(); ?></h3>
<?php the_content(); ?>
<?php endwhile;
else: echo '<p>Blank.</p>'; endif; ?>
<?php pagination(); ?>
You have to mention post type also as per the example
Hope this work for you.

Wordpress Custom post page with pagination

I have custom post types called clients, need to display 5 clients per page with pagination. The page what i have is page-clients.php
I have used the wp_pagenavi plugin.
I get a perfect navigation list 1,2,3 etc etc but on clicking them takes me to page not found
My Code
$args = array(
'posts_per_page' => 5,
'post_type' => 'clients',
'paged' => get_query_var('page')
);
query_posts($args);
<?php while ( have_posts() ) : the_post(); ?>
.....
<?php endwhile; // end of the loop. ?>
<?php wp_pagenavi(); ?>
<?php wp_reset_query();?>
Heres the way you can do it without pagination plugin :) using WP_QUERY instead of query_posts
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1; // setup pagination
$the_query = new WP_Query( array(
'post_type' => 'clients',
'paged' => $paged,
'posts_per_page' => 5)
);
while ( $the_query->have_posts() ) : $the_query->the_post();
echo '<div>' . get_the_title() . '</div>';
the_content();
endwhile;
echo '<nav>';
echo '<div>'.get_next_posts_link('Older', $the_query->max_num_pages).'</div>'; //Older Link using max_num_pages
echo '<div>'.get_previous_posts_link('Newer', $the_query->max_num_pages).'</div>'; //Newer Link using max_num_pages
echo "</nav>";
wp_reset_postdata(); // Rest Data
Pagination Like : Prev 1 2 3 Next
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$data= new WP_Query(array(
'post_type'=>’YOUR_POST_TYPE’, // your post type name
'posts_per_page' => 5, // post per page
'paged' => $paged,
));
if($data->have_posts()) :
while($data->have_posts()) : $data->the_post();
// Your code
endwhile;
$total_pages = $data->max_num_pages;
if ($total_pages > 1){
$current_page = max(1, get_query_var('paged'));
echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'format' => '/page/%#%',
'current' => $current_page,
'total' => $total_pages,
'prev_text' => __('« prev'),
'next_text' => __('next »'),
));
}
?>
<?php else :?>
<h3><?php _e('404 Error: Not Found', ''); ?></h3>
<?php endif; ?>
<?php wp_reset_postdata();?>
Would you please try above code?

Resources