Quantcast
Channel: Question and Answer » meta-query
Viewing all articles
Browse latest Browse all 36

Ignore sticky posts if post is not in meta query

$
0
0

I have such a wp query args:

$args = array(
  'post_type'=> 'myposttype',
  'posts_per_page' => 10,
  'meta_query' => array(
  'relation' => 'AND',
   array('key' => 'routeFrom','value' => 'rome','compare' => 'LIKE'),
   array('key' => 'routeTo','value' => 'paris','compare' => 'LIKE'),
   )    
);

Each my post has custom field routeFrom and routeTo.
In this example if routeFrom equal to Rome and routeTo equal to Paris wp query displaying those posts.

The problem is that sticky posts appear no matter what meta query i’m using.

I want to display particular sticky posts if they only match meta query.

Real life example would be:
I have a route search page where i would like to display sticky post on top of all posts, but only if it ALSO meets meta query (in this case particular route).

I’m using sticky posts plugin “Sticky Custom Post Types”, but posts are stored in the global ‘sticky_posts’ option field, just like regular posts.

Any help?

Updated code:

$stickyargs = array(
  'post_type'=> 'trips',
  'posts_per_page' => -1,
  'post__in' => get_option( 'sticky_posts' ),
  'meta_query' => array(
  'relation' => 'AND',
   array('key' => 'routeFrom','value' => $_GET['from'],'compare' => 'LIKE'),
   array('key' => 'routeTo','value' => $_GET['to'],'compare' => 'LIKE'),
   )    
);

$args = array(
  'post_type'=> 'trips',
  'paged'    => $paged,
  'posts_per_page' => 10,
  'post__not_in' => get_option( 'sticky_posts' ),
  'meta_query' => array(
  'relation' => 'AND',
   array('key' => 'routeFrom','value' => $_GET['from'],'compare' => 'LIKE'),
   array('key' => 'routeTo','value' => $_GET['to'],'compare' => 'LIKE'),
   )    
);

$search = new WP_Query($args);
$searchsticky = new WP_Query($stickyargs);
?>

<?php if ( $searchsticky->have_posts() ) : ?>
    <?php while ( $searchsticky->have_posts() ) : $searchsticky->the_post(); ?>
        <div class="search_item">
            <?php the_content(); ?>>
        </div>
    <?php endwhile; ?>
    <?php else : ?>
        Not found
<?php endif; wp_reset_query(); ?>

<?php if ( $search->have_posts() ) : ?>
    <?php while ( $search->have_posts() ) : $search->the_post(); ?>
        <div class="search_item">
            <?php the_content(); ?>>
        </div>
    <?php endwhile; ?>
    <?php else : ?>
        Not found
<?php endif; wp_reset_query(); ?>

Viewing all articles
Browse latest Browse all 36

Trending Articles