Navigation
This is archived content. Visit our new forum.

Tagged: 

  • Author
    Posts
  • #1149656
    anncivol
    Participant

    Hello,

    I would like to pass arguments in the query that generates categories or archives pages, but I don´t know where this query is created.

    I need something like

    <?php 
    $args = array(
    	'post_status' => array( 'publish', 'inherit'),
    	'post_type' => array( 'post', 'attachment')	
    );
    $query = new WP_Query( $args ); ?>

    in order to retrieve attachments, besides regular posts, in a category page.

    However, when I look at global>_index.php, I don´t see the query there. I expected to have <?php if ( $query->have_posts() ) : ?>
    but there is only
    <?php if ( have_posts() ) : ?>

    It looks like the query has been imported from somewhere else, not created inside _index.php .

    Please tell me in which file/function the query for categories is formed, so that I can include my arguments there.

    P.S: I already know about child themes, have set one and will apply the changes to it.

    Thank you in advance!

    #1149703
    Thai
    Moderator

    Hi There,

    You don’t have to edit the template files, just add the following code under functions.php file in your child theme:

    function x_custom_filter($query) {
    	if( !is_admin() && $query->is_main_query() ) {
    		if( $query->is_archive() ) {
    			$query->set('post_type', array( 'post', 'attachment' ) );
    			$query->set('post_status', array( 'publish', 'inherit' ) );
    		}
    	}
    }
    add_action('pre_get_posts','x_custom_filter');

    Hope it helps 🙂

    #1149742
    anncivol
    Participant

    Worked perfectly! Thank you very very much!!! 🙂

    #1149782
    Thai
    Moderator

    Glad it worked 🙂

    If you need anything else please let us know.

  • <script> jQuery(function($){ $("#no-reply-1149656 .bbp-template-notice, .bbp-no-topic .bbp-template-notice").removeClass('bbp-template-notice'); }); </script>