Tag Info

Hot answers tagged

5

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

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

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 ...


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', ...


3

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 ...


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

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

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 ...


2

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 = ...


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

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

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 ...


2

Declare the following function in your functions.php function wpse58346_wp_list_pages( $pages, $r ) { foreach( $pages as $key => $page ) { if ( 50 < $page->menu_order ) unset($pages[$key]); } return $pages; } Now before calling wp_list_pages() apply a filter as follows add_filter('get_pages', ...


2

$args = array( 'meta_key' => 'home_post_id', 'orderby' => 'meta_value_num', 'order' => 'DESC', 'meta_query' => array( array( 'key' => 'home_post_id', 'value' => 0, 'compare' => '>', 'type' => 'numeric', ) ) ); query_posts($args); Something ...


2

query_posts will clobber the main query. You've overwritten the original set of posts. Don't use query_posts Instead create a new WP_Query object for your second set of posts, and another Loop. It sounds like what you are trying to do is overly complicated. "Mixing" two queries is not likely to be worth the effort. It appears that you are going to have two ...


1

This one is even more simple. Refer to the WP_Query() category parameters. I think you will want to use 'category__not_in'. foreach ( $random_posts_cat_array as $random_post_cat ) { if ( $post_cat_slug == $random_post_cat ) { // No posts from this category! $random_posts_query_args['category__not_in'] = $post_cat_id; } } Edit ...


1

First, do not use query_posts() for secondary loops. The query_posts() function is intended only to modify the primary loop query. Use WP_Query() or get_posts() for secondary loop queries. Also, showposts is deprecated. Use posts_per_page instead. Let's use WP_Query(), as it will be the most analogous to your current implementation: <?php // First, ...


1

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 ...


1

try: global $query_string; query_posts( $query_string . '&cat=-81' ); http://codex.wordpress.org/Function_Reference/query_posts#Preserving_Existing_Query_Parameters


1

Something like this would work $cat = ( !empty( $wp_query->get('cat') ) ) ? wp_query('cat').',-81' : '-81'; $wp_query->set( 'cat', $cat ); But what you really want to be doing is using tax_query and just setting the taxonomy parameter to category.


1

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, ...


1

Before the line <?php if(have_posts()): ?> Insert something like this <?php query_posts($query_string . '&cat=-4'); ?> This excludes the category with Category ID 4. As seen here


1

If retrieve_cat_data_sp is the function from this question then that returns categories except the ones given in the argument: $cat_lists = retrieve_cat_data_sp('81'); It essentially a wrapper for get_categories. So to exclude multiple Ids $cat_lists = retrieve_cat_data_sp('81,82'); If you want to exclude 81 as well as those IDs listed in ...


1

I solved this by using this code function exclude_thumbnail_from_gallery($null, $attr) { if (!$thumbnail_ID = get_post_thumbnail_id()) return $null; // no point carrying on if no thumbnail ID // temporarily remove the filter, otherwise endless loop! remove_filter('post_gallery', 'exclude_thumbnail_from_gallery'); // pop in our ...


1

The asker refused to post his solution as answer, so I took the liberty to get this question out of our growing Unanswered Questions list … <?php $logo = get_post_meta($post->ID, '_lm_comm_logo', true); $map = get_post_meta($post->ID, '_lm_comm_map', true); $args = array( 'numberposts' => -1, 'orderby' => 'menu_order', ...


1

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 ...


1

What you want, I think, is the posts_where filter, which allows you to add additional WHERE clauses to the SQL query that get_posts generates. This will save you the trip to the database to fetch only parent posts. The hooked function will receive two arguments: the WHERE clause itself and the query object. Check to see if the query is for the city post ...


1

According to the codex for WP_Query, the solution might be to use the post_parent as the id of the top level post. But as there are a whole lotta top level cpts in your case, I think this solution should help you: How to display only top level posts in loop via WP_Query?. Update: <?php $all = get_posts(array('post_type'=> 'city', 'posts_per_page' ...



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