How to use pre_get_posts on archive page custom post type
I'm trying to use pre_get_posts to modify my post display request. The posts I want to display are custom post types. I am in a custom page template.
I have this is my functions.php
function tax_and_offset_homepage( $query ) {
if ( !is_admin() $query-is_post_type_archive( array( 'project' ) ) ) {
$query-set( 'post_type', 'project' ) ;
if (null!==(get_option('sticky_posts'))){
$count_sticky = count(get_option('sticky_posts'));
}
$ppp = get_option( 'posts_per_page' );
$offset = $count_sticky;
if ( !$query-is_paged() ) {
$query-set( 'posts_per_page', $ppp - $offset );
} else {
$offset = ( ( $query-query_vars['paged']-1 ) * $ppp ) - $offset;
$query-set( 'posts_per_page', $ppp );
$query-set( 'offset', $offset );
}
}
}
add_action('pre_get_posts','tax_and_offset_homepage');
function homepage_offset_pagination( $found_posts, $query ) {
if (null!==(get_option('sticky_posts'))){
$count_sticky = count(get_option('sticky_posts'));
}
$offset = $count_sticky;
if( $query-is_post_type_archive( array( 'project' ) ) ) {
$found_posts = $found_posts + $offset;
}
return $found_posts;
}
add_filter( 'found_posts', 'homepage_offset_pagination', 10, 2 );
And in my custom template page :
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$params = ['post_type' = 'project', 'order' = 'DESC', 'posts_per_page' = 9, 'paged' = $paged];
$my_query = new WP_Query( $params );
foreach ($children_array as $project) : ?
div
?php echo get_the_title($project); ?
/div
?php endforeach; ?
Problem : the code in functions.php has NO EFFECT. how can I act on my request with the pre_get_posts?
Topic pre-get-posts page-template custom-post-types Wordpress
Category Web