WP How can I combine multipe query? - wordpress

I like to combine multiple query into one result.
$args_a and $args_b in below code are simplified to summarize my question. I like to combine 2 or more query in one result.
They should not be overwritten each other, so I think "merge" is not proper method to combine this case.
Now, how can I combine them together?
<?php
$args_a = array(
'posts_per_page' => 9,
'paged' => $paged,
'category_name' => 'shinmatsudo',
'category__in' => array( 227 ),
'category__not_in' => array( 3 ),
'meta_query' => array(
'relation' => 'AND',
array(
'key' => '1b',
'compare' => 'NOT EXISTS'
),
array(
'key' => '1d',
'compare' => 'NOT EXISTS'
),
),
);?>
<?php
$args_b = array(
'paged' => $paged,
'posts_per_page' => 9,
'category_name' => 'matsudo',
'category__in' => array( 329 ),
'category__not_in' => array( 3 ),
'meta_query' => array(
'relation' => 'and',
array(
'key'=> '2a',
'value' => array('2020-02-01' , '2020-06-01'),
'compare' => 'BETWEEN',
'type' => 'DATE',
),
),
);
?>
<?php
global $post;
$my_posts= get_posts($args_b);
$my_posts= get_posts($args_a);
foreach($my_posts as $post):setup_postdata($post);?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<h1 class="entry-title"><?php the_title(); ?></h1>
</div>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>

The problem your code has is you are overwriting $my_posts variable. Think about what your code is doing:
global $post;
$my_posts= get_posts($args_b);
$my_posts= get_posts($args_a);
You are reassigning $my_posts to the second get_posts so the $args_b will always be overwritten.
You can try using array_merge() to take the results of both get_posts and combine them into a single array.
global $post;
$my_posts= array_merge( get_posts($args_b), get_posts($args_a) );
foreach($my_posts as $post):setup_postdata($post);?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<h1 class="entry-title"><?php the_title(); ?></h1>
</div>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>

Related

Wordpress - two loops in CPT archive page, exclude posts from main query

I have two loops in my archive-inzerat.php, first is custom loop, second is default loop:
<div class="container">
<div class="row">
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$featured_args = array(
'post_type' => 'inzerat',
'post_status' => 'publish',
'posts_per_page' => 10,
'paged' => $paged,
'orderby' => 'publish_date',
'order' => 'DESC',
'meta_query' => array(
array(
'key' => 'is_featured',
'value' => '1',
)
)
);
$featured_query = new WP_Query( $featured_args );
if ( $featured_query->have_posts() ) : ?>
<div class="col-md-12">
<div class="row">
<?php
while ( $featured_query->have_posts() ) : $featured_query->the_post();
echo '<div class="col-md-3">';
get_template_part( 'content', 'inzerat' );
echo '</div>';
endwhile;
wp_reset_postdata();
?>
</div>
</div>
<?php
endif;
?>
<?php
if ( have_posts() ) : ?>
<div class="col-md-12">
<div class="row">
<?php
while ( have_posts() ) : the_post();
echo '<div class="col-md-3">';
get_template_part( 'content', 'inzerat' );
echo '</div>';
endwhile;
?>
</div>
<div class="clearfix"></div>
<div class="utf-pagination-container margin-top-20">
<nav class="pagination">
<?php
global $wp_query;
$total_pages = $wp_query->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' => '<i class="fa fa-angle-left"></i>',
'next_text' => '<i class="fa fa-angle-right"></i>',
'type' => 'list',
'end_size' => 3,
'mid_size' => 3,
) );
}
wp_reset_postdata();
?>
<script>
jQuery('ul.page-numbers').addClass('pagination-custom');
//jQuery('ul.page-numbers li').addClass('page-item');
//jQuery('ul.page-numbers li a, ul.page-numbers li span').addClass('page-link');
jQuery('ul.page-numbers li span.current').parent().addClass('active');
</script>
</nav>
</div>
</div>
<?php
endif;
?>
</div>
</div>
In first loop I want to display only custom posts by custom field is_featured which is type boolean. In second loop I want to exclude these posts and display others to prevent duplications. Unfortunately, this code I use removes everything from the main loop:
function my_posts_filter( $query ) {
if ( ! is_admin() && $query->is_main_query() && is_post_type_archive( 'inzerat' ) ) {
$query->set( 'meta_query', array(
array(
'key' => 'is_featured',
'value' => '1',
'compare' => 'NOT IN'
)
));
}
}
add_action( 'pre_get_posts', 'my_posts_filter' );
What's wrong here?
I want also ask, will my pagination work and not break when adding custom loop?
Thanks for helping.
As per my opinion try this logic,
Define a blank array at the beginning of your file.
$firstloop_ids = array();
When your first loop is performed, you just need to add those IDs into that blank array. ( you can use array_push for that, reference: https://www.w3schools.com/php/func_array_push.asp )
For the second loop, you can exclude those ids using that array by this parameter.
'post__not_in' => $firstloop_ids,
This should work for you!
Try with post__not_in argument:
$featured_args = array(
'post_type' => 'inzerat',
'post_status' => 'publish',
'posts_per_page' => 10,
'paged' => $paged,
'orderby' => 'publish_date',
'order' => 'DESC',
'post__not_in' => array(278),
'meta_query' => array(
array(
'key' => 'is_featured',
'value' => '1',
)
)
);
It will be definitely exclude specific posts.

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.

Wordpress Query not Working

I am trying to use this code to pull out posts that have the PackageID 3, however it just doesn't seem to work and pulls out any post instead.
What am I missing?
<?php
$args = array(
'orderby' => 'rand',
'order' => 'ASC',
'meta_query' => array(
'key' => 'packageID',
'value' => '3',
'compare' => '=',
'type' => 'NUMERIC',
),
);
query_posts($args); ?>
<?php while (have_posts() ) : the_post(); ?>
<?php the_title(); ?>
<?php endwhile; ?>
I think it's because the meta_query needs to be an array inside an array, so the code would look like
<?php
$args = array(
'orderby' => 'rand',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'packageID',
'value' => '3',
'compare' => '=',
'type' => 'NUMERIC',
)
),
);
query_posts($args); ?>
<?php while (have_posts() ) : the_post(); ?>
<?php endwhile; ?>
https://codex.wordpress.org/Class_Reference/WP_Query

Wordpress custom query missing two posts

I am running a custom query but when returning posts_per_page, it is always two behind. I am using a custom post type called events, that is pulling in the code to display the posts from the functions file. I also want to implement pagination(not working also), will this be a problem?
I have ran var dump which returns everything in the query including posts before the current date. I imagine because it's dumping before the if statement? Any ideas?
Any ideas?
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
//Current exhibitions
$args = array(
'post_type' => 'exhibitions',
'orderby' => 'exhibition_start_date',
'meta_key' => 'exhibition_start_date',
'order' => 'ASC',
'posts_per_page' => 6,
'paged' => $paged
);
$my_query = new WP_Query( $args );
while ( $my_query->have_posts() ) : $my_query->the_post();
$exhibition_start_date = get_post_meta($post->ID, 'exhibition_start_date', true);
//Current
if ($exhibition_start_date >= date('Y-m-d') ) {
get_exhibition_container();
}
endwhile;
wp_reset_postdata();
?>
New code: (I've also added in my initial loop which brings in the page content, thought it could interfear?)
<div role="main" class="clearfix">
<div class="one-column">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
<?php endwhile; else: ?>
<?php endif; ?>
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
//Current exhibitions
$args = array(
'post_type' => 'exhibitions',
'meta_query' => array(
array (
'key' => 'exhibition_start_date',
'value' => date('Y-m-d'),
'compare' => '>='
)
),
'orderby' => 'exhibition_start_date',
'meta_key' => 'exhibition_start_date',
'order' => 'ASC',
'posts_per_page' => 10,
'paged' => $paged
);
$my_query = new WP_Query( $args );
while ( $my_query->have_posts() ) : $my_query->the_post();
$exhibition_start_date = get_post_meta($post->ID, 'exhibition_start_date', true);
//Current
get_exhibition_container();
endwhile;
wp_reset_postdata();
?>
<div class="previous-holder">
<?php previous_posts_link(); ?>
</div>
<div class="next-holder">
<?php next_posts_link(); ?>
</div>
Instead of using a if condition in your loop, you should use meta_query param :
$args = array(
'post_type' => 'exhibitions',
'meta_query' => array(
array(
'key' => 'exhibition_start_date',
'value' => date('Y-m-d'),
'compare' => '>='
)
),
'orderby' => 'exhibition_start_date',
'meta_key' => 'exhibition_start_date',
'order' => 'ASC',
'posts_per_page' => 6,
'paged' => $paged
);
EDIT : About pagination
previous_posts_link codex :
Prints a link to the previous set of posts within the current query.
You should try using previous_posts_link() and next_posts_link() before wp_reset_postdata()

Getting posts from categories array

I have some certain categories' ids. I want to loop this categories and last 3 posts in one time. I try this but only come one category from array.
<?php
$args = array(
'cat' => 48,43,49,46,47,44,51,50,42,
'order' => 'ASC',
'showposts' => 3
);
query_posts($args);
?>
<?php while (have_posts()) : the_post(); ?>
<?php the_title(); ?>
<?php endwhile; ?>
This piece of code won't work: 'cat' => 48,43,49,46,47,44,51,50,42,
You'll have to use an array 'cat' => array(48,43,49,46,47,44,51,50,42),
For some reason 'cat' did not work. We used
'category__in' => array( 2, 6 ),
and it workined fine.
The completed working code:
<?php
// -----------------------------
$args = array(
'post_type' => 'post',
'order' => 'ASC',
'category__in' => array(2,6)
);
$query = new WP_Query( $args );
?>
You can get All Posts in a Category which one you want publish.
query_posts( array ( 'category_name' => 'my-category-slug', 'posts_per_page' => -1 ) );
You can find post as per your expectation by.
query_posts( array ( 'category_name' => 'carousel', 'posts_per_page' => 10, 'orderby' => 'date', 'order' => 'DESC' ) );
According to your code. Update --
<?php
$args = array(
'cat' => [48,43,49,46,47,44,51,50,42], //change here array
'order' => 'ASC',
'posts_per_page' => 3 //showposts deprecated now
);
query_posts($args);
?>
<?php while (have_posts()) : the_post(); ?>
<?php the_title(); ?>
<?php endwhile; ?>
<?php wp_reset_query(); ?> // you should reset your query
should actually be:
'cat' => '48,43,49,46,47,44,51,50,42'

Resources