Get posts by meta data OR title
As with most questions involving OR clauses, the answer is the same: use a custom query or alter WP_Query using the ‘posts_clauses’ filter.
As with most questions involving OR clauses, the answer is the same: use a custom query or alter WP_Query using the ‘posts_clauses’ filter.
WP_Query->is_main_query() method source (which function of same name calls) is very short and simple: function is_main_query() { global $wp_the_query; return $wp_the_query === $this; } The main query is the query that is stored in $wp_the_query global. But what is that global? When WP sets up main query it stores it in two places: $wp_the_query and … Read more
Few days ago I wrote a quick solution using pre_get_posts filter to hide some pages in admin area. Maybe you could use it as a good starting point for whatever you’d like to achieve. if ( is_admin() ) add_filter(‘pre_get_posts’, ‘mau_filter_admin_pages’); function mau_filter_admin_pages($query) { $query->set(‘post__not_in’, array(1,2,3) ); // Do not display posts with IDs 1, 2 … Read more
Use pre_get_posts action hook function archive_meta_query( $query ) { if ( $query->is_archive){ $query->query_vars[“meta_key”] = ‘wolf’; $query->query_vars[“meta_value”] = ‘boltz’; } } add_action( ‘pre_get_posts’, ‘archive_meta_query’, 1 );
Firstly, you shouldn’t use post_category, because according to the wordpress codex wp_insert_post(): ‘post_category’ => [ array(, <…>) ] //post_category no longer exists, try wp_set_post_terms() for setting a post’s categories Secondly, if you want the post title to be your slug you shouldn’t need to use the post_name parameter, because it gets constructed from the title … Read more
You’re nearly there, you just need to tell WP that you want to query posts as well as your Press CPT. So: query_posts( array( ‘post_type’ => array(‘posts’, ‘portfolio’), …); where portfolio is the name of your custom post type. The relevant Codex page [Update] So this is how the query should look <?php $args = … Read more
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 … Read more
query_posts() is useful in cases when there is no main query: calls to wp-admin/admin-ajax.php, wp-admin/admin-post.php or wp-login.php for example. Yes, you can achieve the same results there without query_posts() and slightly less compact code instead. But if you don’t have to care about side effects, using query_posts() is acceptable.
Custom fields are saved in the post_meta table. In your query you got the post title and post ID, so now you have to get post meta. Use: <?php get_post_meta( get_the_ID(), ‘_location’, true ); ?> same for the rest of your custom fields only ‘_location’ will change according to field you are getting. Read more … Read more
A friend helped me with a solution that I could place in my functions.php file: add_filter( ‘pre_get_posts’, ‘my_pre_get_posts’ ); function my_pre_get_posts( $query ) { if ( is_tax( ‘locations’ ) && empty( $query->query_vars[‘suppress_filters’] ) ) $query->set( ‘orderby’, ‘comment_count’ ); return $query; }