Exclude post on Wordpress loop through meta-key - php

In my wordpress site, I use a custom field/meta-key called "offline".
I would like to exclude from the basic loop (in tag.php and category.php) all the posts that have the custom field/meta-key "offline" set to "true". Can someone help me? Thank you.
This is my code currently:
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
... Display post content
<?php endwhile; ?>
<?php endif; ?>

$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'offline',
'value' => 'true',
'compare' => '!='
)
)
);
$query = new WP_Query($args);
if ($query->have_posts()) :
while ($query->have_posts()) :
$query->the_post();
$post_id = get_the_ID();
endwhile;
endif;
So all posts with meta_key=offline and meta_value=true is not included in loop.

Exclude all the posts that have this meta-checkbox
'meta_query' => array(
array(
'key' => 'meta-checkbox',
'value' => '1',
'compare' => '<'
)
),
This worked for me.

Related

Wordpress Custom Post Loop can't exclude category

I am trying to exclude a category from a custom post type loop but with no luck. I have no idea why this isn't working. I have looked all over the internet but no luck, please can someone help identify the issue.
I have a category with an ID of 141.
I am running through a loop of posts where I am trying to exclude posts assigned to that category but they are still showing up in the loop. Where am I going wrong? Here is my code.
<?php
$args = array(
'post_type' => 'programme',
'cat' => -141,
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
?>
<div class="grid-item">
<?php the_title(); ?>
</div>
<?php
endwhile;
else:
_e( 'Sorry, no posts matched your criteria.', 'textdomain' );
endif;
wp_reset_postdata();
?>
For anyone with a similar issue, I managed to fix it by using this code instead.
$args = array(
'post_per_page' => '-1',
'post_type' => 'programme',
'tax_query' => array(
array(
'taxonomy' => 'ticket-category',
'field' => 'slug',
'terms' => array( 'savers-tickets' ),
'operator' => 'NOT IN',
)
),
);

Group by a custom value field wordpress posts

I have several posts with a custom field named "series". I want to group all posts by this custom field and below of this i want to list all posts which have not this custom field.
First i wanted to get grouped custom field value and then to be able to query again for all posts with this custom value key and value. But even trying to get the unique custom values does not work.
What i tried is this:
<?php
function query_group_by_filter($groupby){
global $wpdb;
return $wpdb->postmeta . '.meta_key = "series"';
}
?>
<?php add_filter('posts_groupby', 'query_group_by_filter'); ?>
<?php $states = new WP_Query(array(
'meta_key' => 'series',
'ignore_sticky_posts' => 1
));
?>
<?php remove_filter('posts_groupby', 'query_group_by_filter'); ?>
<ul>
<?php
while ( $states->have_posts() ) : $states->the_post();
$mykey_values = get_post_custom_values( 'series' );
foreach ( $mykey_values as $key => $value ) {
echo "<li>$key => $value ( 'series' )</li>";
}
endwhile;
?>
</ul>
Whats wrong with this code?
WP_Meta_Query
All posts with custom value:
<?php
$args = array(
'post_type' => 'my-post-type',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'series',
'value' => 'my-val',
'compare' => '='
),
array(
'key' => 'series',
'value' => '',
'compare' => '!='
)
)
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) : ?>
<ul>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li id="post-<?php the_ID(); ?>">
<?php the_title(); ?>
</li>
<?php
endwhile;
wp_reset_postdata(); ?>
</ul>
<?php endif; ?>
And without values:
<?php
$args = array(
'post_type' => 'my-post-type',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'series',
'value' => '',
'compare' => '='
)
)
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) : ?>
<ul>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li id="post-<?php the_ID(); ?>">
<?php the_title(); ?>
</li>
<?php
endwhile;
wp_reset_postdata(); ?>
</ul>
<?php endif; ?>

WP: Simplify a glossary php script/code

I would like to simplify my code to avoid any repetition of the same code format. I did a custom post type of Glossary from A-Z, though i did a basic custom Query Post but it end-up to messy code output.
<?php
$args = array (
'post_type' => array( 'cre-library' ),
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
'meta_query' => array( array( 'key' => 'post_category', 'value' => 'A', 'compare' => 'LIKE', 'type' => 'CHAR' ) )
);
$query = new WP_Query ( $args );
if ($query->have_posts()) :
echo '<div id="content-for-a" class="active">';
echo '<h2>A</h2>';
while ($query->have_posts()) : $query->the_post();
the_title('<h3>','</h3>');
the_content();
endwhile;
echo '</div>';
endif;
wp_reset_postdata();
?>
<?php
$args = array (
'post_type' => array( 'cre-library' ),
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
'meta_query' => array( array( 'key' => 'post_category', 'value' => 'B', 'compare' => 'LIKE', 'type' => 'CHAR' ) )
);
$query = new WP_Query ( $args );
if ($query->have_posts()) :
echo '<div id="content-for-b">';
echo '<h2>B</h2>';
while ($query->have_posts()) : $query->the_post();
the_title('<h3>','</h3>');
the_content();
endwhile;
echo '</div>';
endif;
wp_reset_postdata();
?>
<?php //...and so on ?>
This code is working already. if we can simplify this into one loop block of PHP with having it matched post_category value per entry and display it all the post at the same time.

How do I add order & orderby parameters to this wordpress query?

I managed to paint myself into a corner when using this snippet, but I can't manage to work out how to sort the query. Usually I can do it but with this snippet that excludes one tag on the tag page I can't really work it out. Anyone?
$exclude_tags = array(17);
global $wp_query;
$wp_query->set('tag__not_in', $exclude_tags);
$wp_query->get_posts();
if (have_posts()) : while (have_posts()) : the_post();
This example will be help full for you :-
$args = array(
'post_type' => 'post',
'meta_key' => 'pb_issue_featured',
'orderby' => 'meta_value',
'order' => 'DESC',
'posts_per_page' => $posts,
'paged' => $paged,
'paged' => 1,
'meta_query' => array(
array(
'key' => 'headline',
'value' => 1,
'compare' => '!='
)
)
);
add_filter( 'posts_orderby', 'filter_query' );
$q = new WP_Query($args);
remove_filter( 'posts_orderby', 'filter_query' );
function filter_query( $query ) {
$query .= ', wp_posts.menu_order ASC';
return $query;
}
Referenced From
Please look at this example. You can do like this.
The code will display the title of last ten posts sorted alphabetically in ascending order.
<?php
$args = array( 'posts_per_page' => 10, 'order'=> 'ASC', 'orderby' => 'title' );
$postslist = get_posts( $args );
foreach ( $postslist as $post ) :
setup_postdata( $post ); ?>
<div>
<?php the_title(); ?>
</div>
<?php
endforeach;
wp_reset_postdata();
?>
Please refer the link http://codex.wordpress.org/Template_Tags/get_posts for more details.

Custom Taxonomy WP_Query

I am trying to display a custom post type that has custom taxonomy, but I am not having any luck. Nothing is showing up. I appreciate any help.
Post type = gallery
Custom Taxonomy slug = photoarea
The 'photoarea' I want to display = fourth
<?php
$args = array(
'post_type' => 'gallery',
'tax_query' => array(
array(
'taxonomy' => 'photoarea',
'field' => 'fourth',
)
),
'posts_per_page' => 10,
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();
the_post_thumbnail();
endwhile; endif;
wp_reset_query();
?>
You may use below code snippet:
$the_query = new WP_Query( 'post_type=gallery&photoarea=fourth');
and then your while loop.
if my understanding is right, that you need to get the custom taxonomy with following code,
instead of field you must use term to get the posts in Fourth
<?php
$args = array(
'post_type' => 'gallery',
'tax_query' => array(
array(
'taxonomy' => 'photoarea',
'field' => 'slug',
'terms' => 'fourth'
)
),
'posts_per_page' => 10,
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();
the_post_thumbnail();
endwhile; endif;
wp_reset_query();
?>
$args = array('post_type' => 'gallery','posts_per_page'=>'-1','tax_query' => array(array(
'taxonomy' => 'photoarea',
'field' => 'term_id',
'terms' => $your_term_id,
),
),
);

Categories