Articles of pre get posts

Using pre_get_posts to filter out posts, pagination problem

I’m trying to figure out how to fix my pagination problem while filtering out posts on the fly. I have a dropdown box just above the category archive that allows a user to sort or filter out posts based on custom fields. The posts, filter just fine on the first page. My problem shows up […]

pre_get_posts post_meta event

I wish pre_get_posts to filter posts that appear in the archive-events.php and taxonomy-eventcat.php templates. I have a post_meta in use named o_event_start that stores a value in DATETIME (2014-12-31 21:30:00) format and I wish to show all events (cpt) that are happening today (only on page 1) using WP_Query are happening tomorrow (only on page […]

Pre Get Posts – Difference between conditional() and $query->conditional()?

This may be a poor question but inside pre_get_posts() is there any real difference between $query->conditional() versus just adding the conditional() ? Take the following example: /** Themes Pre Get Posts **/ function theme_pgp( $query ) { if( $query->is_home() ) { // Run Conditional 1 } if( is_home() ) { // Run Conditional 2 } […]

Sort WordPress Archive by multiple oderby arguments in pre_get_posts action

Actually this private function in my function.php works, BUT: i often get Error 500 by the Server on the first load, often it renders AFTER reloading the page. ## change the sorting order on archive pages: order archive by title add_action( ‘pre_get_posts’, ‘gpsp_change_sort_order’); function gpsp_change_sort_order($query) { if(is_archive() && $query->is_main_query()) : $query->set( ‘meta_query’, array( ‘relation’ => […]

Use pre_get_posts to display search results by meta in specific order

I have a search query that I’m limiting to search within a custom post type. When the content is returned I have it sorting by meta value. The post meta is a person’s position. The function below works as expected, but I need to pull the posts and sort them in a certain order. function […]

Why query by specific date with variables doesn't return same result that with harcoded integers?

Having a blog with one post per day from monday to friday, on weekends I need to retrieve last friday post if user click the “today word” or the las thursday post if user click the “yesterday word”. I have registered a query var with this code: function register_query_vars ($vars) { $vars[] = ‘dia’; return […]

Search pre_get_posts filter which can handle multiple post types and categories?

I have a Search which allows users to search multiple custom post types (CPT). That works fine. BUT I would like the query to exclude specific tags from each of the CPTs. Eg. Include ( (Posts excluding category X) AND all CPTs of post_type ‘doc’) which have the given search term. I know how to […]

Adding a parameter to the default query

I’m using ACF to create a “Featured” post section of the index.php and I’m having trouble then excluding that post from the main query. <?php $tmp = $posts; $isPaged = (get_query_var(‘paged’) == 0) ? false : true; $posts = get_posts(array( ‘numberposts’ => 1, ‘post_type’ => ‘post’, ‘meta_key’ => ‘featured_post’, ‘meta_value’ => true )); if( !$isPaged […]

Child pages not affected by orderby

My loop won’t order the child pages I’m displaying in the order I want them to, neither ASC or DESC. I’ve already seen many other similar issues, but those were resolved with disabling plugins (which I don’t have) or removing or adding quotes (which I’ve checked multiple times). <?php $args = array( ‘post_type’ => ‘page’, […]

Sort custom post types by last name in the backend

I have a custom post type called speakers, where post title is a speakers first- AND last name. I created it with this code: add_action( ‘init’, ‘create_post_type’ ); function create_post_type() { register_post_type( ‘speaker’, array( ‘labels’ => array( ‘name’ => __( ‘Speakers’ ), ‘singular_name’ => __( ‘Speaker’ ) ), ‘public’ => true, ‘has_archive’ => true, ‘supports’ […]