Hot answers tagged

17

Instead of using the code from the question in functions.php, replace it with this: /** * Prevent certain plugins from receiving automatic updates, and auto-update the rest. * * To auto-update certain plugins and exclude the rest, simply remove the "!" operator * from the function. * * Also, by using the 'auto_update_theme' or 'auto_update_core' ...


12

Method 1 You can add a constructor to your custom Walker to store some additional exclusion arguments, like: class custom_nav_walker extends Walker_Nav_Menu { function __construct( $exclude = null ) { $this->exclude = $exclude; } function skip( $item ) { return in_array($item->ID, (array)$this->exclude); // or ...


9

you could use wp_parse_args() to merge your arguments into the default query // Define the default query args global $wp_query; $defaults = $wp_query->query_vars; // Your custom args $args = array('cat'=>-4); // merge the default with your custom args $args = wp_parse_args( $args, $defaults ); // query posts based on merged arguments query_posts($...


9

The basic explanation You have a template tag that is called is_search() to determin if you're on a search page or not. This then calls get_search_template() which basically is a wrapper function for get_query_template('search'). When you look into the last function, then you'll see that it basically does locate_template(), which checks for file ...


5

First off, note that, since your custom loop is a secondary loop/query, you should use the WP_Query class instead of query_posts(). Read why. That being said, /* main post's ID, the below line must be inside the main loop */ $exclude = get_the_ID(); /* alternatively to the above, this would work outside the main loop */ global $wp_query; $exclude = $...


5

You'll need to filter comments_clauses, since WP_Comment_Query only supports a limited post type == X argument. /** * Exclude comments of the "foobar" post type. * * @param array $clauses * @param object $wp_comment_query * @return array */ function wpse_72210_comments_exclude_post_type( $clauses, $wp_comment_query ) { global $wpdb; if ( ! ...


4

This the post__not_in arg should work dandy for you: $args = array( 'numberposts' => 5, 'offset' => 0, 'category' => 7, 'post__not_in' => array( $post->ID ) ); $myposts2 = get_posts($args);


4

I have rewritten the query_posts. As for get_posts you are better off using the WP_Query due to more control over the tax_query. Explained here. <?php $args = array( 'cat' => 84, 'posts_per_page' => 3, 'offset' => 0, 'tax_query' => array( 'relation' => 'NOT IN', array( 'taxonomy' => 'display', ...


4

The parameter you are looking for is post__not_in (kaiser has a typo in his answer). So the code could be like: <?php $my_query = new WP_Query(array( 'post__not_in' => array(278), 'post_type' => 'case-study', 'paged' => $paged, )); while ($my_query->have_posts()) : $my_query->the_post(); endwhile;


4

Don't use query_posts. Use a filter on pre_get_posts. function no_front_sticky_wpse_98680($qry) { if (is_front_page()) { $qry->set('post__not_in',get_option( 'sticky_posts' )); } } add_action('pre_get_posts','no_front_sticky_wpse_98680'); By running query_posts you clobber the main query, over-writing it with another query. That is why you ...


4

If you don't use any other meta data, just add the following to your $args: 'meta_key' => 'sold', 'meta_value' => true, // or whatever it is you're using here 'meta_compare' => '!=', Otherwise, use a WP_Meta_Query: 'meta_query' => array( array( 'key' => 'sold', 'value' => true, // or whatever it is you're using here ...


4

Excluding categories with pre_get_posts() I found that excluding a category via pre_get_posts() and set_query_var() would work fine except for widgets. The Recent Post Widget would only exclude the category when using $query->set() instead. <?php /** * Does NOT apply to the Recent Posts widget. */ function glck1403271109_exclude_categories( $query ...


4

From a quick look at the documentation for get_posts(), it doesn't look like you can exclude a post based on its category. EDIT: Actually, thanks to AntonChanning, turns out you can - just add &cat=-1 to your argument string, where 1 is the ID of the category you wish to exclude: $postslist = get_posts('numberposts=1&order=DESC&orderby=date&...


4

In addition to the answer by Tim, one can always use a proper tax_query. All the build in tag and category parameters gets converted to a proper tax_query before being passed to the WP_Tax_Query class to build the JOIN clause for the SQL query. I use a tax_query in almost all applications as it gives one a lot of flexibilty, specially when it comes to child ...


3

Rather than performing a separate query, to exclude a category (or any other taxonomy) term, you can hook into pre_get_posts: add_action('pre_get_posts', 'wpse41820_exclude_cat_from_front_page'); function wpse41820_exclude_cat_from_front_page( $query ){ if( $query->is_main_query() && is_front_page() ){ $tax_query = array(array( ...


3

You just need to set the operator parameter to 'NOT IN' (see Codex on tax queries). Untested, but for your purposes: $args = array( 'post_type'=> 'post', 'post_status' => 'publish', 'order' => 'DESC', 'tax_query' => array( array( 'taxonomy' => 'post_format', 'field' => 'slug', '...


3

In your code, if $post_id were, say, 99, this: query_posts("showposts=4&post_type=page&post_parent=168&orderby=rand&exclude='. $post_id .'"); would result in this being passed to query posts: query_posts("showposts=4&post_type=page&post_parent=168&orderby=rand&exclude='. 99 .'"); so, your issue here is '. 99 .' isn't a ...


3

First, don't use query_posts(). Just get rid of the call entirely. It will break things. Second: I tried adding the following before the query_posts ( function but it does nothing. Callbacks and add_action() calls belong in functions.php, not in the template file. If you've put it directly in home.php, remove it from there, and put it in functions.php....


3

Try this in your functions.php, and change news for whatever you want to be blocked in your site. add_action('wp', 'check_search'); function check_search() { global $wp_query; if (!$s = get_search_query()) return false; if (preg_match('/news/', $s)) { $wp_query->set_404(); status_header(404); ...


3

I just tested the below code, and it works: <?php $q1 = new WP_query(array('category_name' => 'test-cat-1', 'order' => 'ASC')); if($q1->have_posts()) : $firstPosts = array(); while($q1->have_posts()) : $q1->the_post(); $firstPosts[] = $post-&...


3

Note as of wordpress 3.8.2 the type of the plugin item passed to this function has changed and it is now an object. /** * @package Plugin_Filter * @version 2.0 */ /* Plugin Name: Plugin Filter Plugin URI: http://www.brideonline.com.au/ Description: Removes certain plugins from being updated. Author: Ben Wise Version: 2.0 Author URI: https://github.com/...


3

The original author isn't quite right in saying "which is merely the more elegant way to write". set_query_var() will always override the main query, whereas if you actually use: $wp_query->set( 'category__not_in', $excluded ); ... it will work for any instance of query_posts(), such as the recent posts widget.


3

All media (somewhat incorrectly) in the $wpdb->posts table will be "attachments" whether actually attached or not. "Attachments" that are actually attached will have a post_parent other than 0, so what you need are all of the attachments that have a 0 in the post_parent column, if I understand you. $args = array( 'post_type' => 'attachment', '...


2

pre_get_posts is the right hook for this since i just did category exclusion in another answer i will post it here too. Exclude the category from the Wordpress loop based on the codex sample: http://codex.wordpress.org/Custom_Queries#Category_Exclusion add_action('pre_get_posts', 'wpa_31553' ); function wpa_31553( $wp_query ) { //$wp_query is ...


2

There's a plugin for that :-) http://wordpress.org/extend/plugins/hide-categories/


2

http://codex.wordpress.org/Function_Reference/is_main_query add_action( 'pre_get_posts', 'foo_modify_query_exclude_category' ); function foo_modify_query_exclude_category( $query ) { if ( $query->is_main_query() && ! $query->get( 'cat' ) ) $query->set( 'cat', '-5' ); } So it's quite obvious to how exclude certain categories ...


2

I'd use wp_list_filter() with the operator 'NOT' to compare either the term's name, slug or ID (depending how you want to test for term to be exluded). Untested but something like this should work (assuming that you want to exclude the term with slug 'myslug'): $terms = wp_get_post_terms( $post->ID, 'wedding_cat'); $terms = wp_list_filter($terms, ...


2

Did you consult the is_page_template() Codex entry? If you want to query for the page.php page template, then you need to pass that filename to is_page_template(); i.e.: <?php if ( ! is_page_template( 'page.php' ) ) { // The current page template is NOT page.php; // do something } ?> Sidenote: if you want to query for being a static page ...


2

If everything else is doing what you want, change this line: $categories = get_categories('hide_empty=0'); To this: $categories = get_categories('hide_empty=0&exclude=10'); Swap '10' with the category number you want to exclude.


2

Based on Johannes’ code, but using the post__not_in argument: /* Secondary query using WP_Query */ $wpse63027_posts = new WP_Query( array( 'category_name' => 'MyCatName', 'posts_per_page' => -1, 'post__not_in' => array( get_queried_object_id() ), // Exclude current post ID (works outside the loop) ) ); Then you can loop through the ...



Only top voted, non community-wiki answers of a minimum length are eligible