Support » Theme: TotalPress » Custom Query

  • Resolved 8edrich

    (@8edrich)



    Hi all, I need some help with custom queries in TotalPress.
    How can I write query to get only posts from an author ‘Bedrich’, where the custom field key is ‘color’ and the custom field value is ‘blue’?
    Is this possible?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Theme Author Thomas

    (@tsquez)

    Hi there,
    I am not sure I understand your question.

    You want to get posts from a specific author using colors?

    No, it was just an example…
    I want to get posts from a specific author, and additionally where my custom field has a specific value. Like this:

    
    <?php
    $args = array(
        'author_name' => 'Bedrich',
        'post_type' => 'my-custom-post',
        'meta_key' => 'color',
        'meta_value' => 'blue');
    $the_query = new WP_Query( $args ); ?>
    
    <?php if ( $the_query->have_posts() ) : ?>
        <!-- the loop -->
        <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
            <h1 href="<?php the_permalink(); ?>"><?php the_title(); ?></h1>
            <?php the_content(); ?>
        <?php endwhile; ?>
    <?php endif; ?>
    

    I dont know, how to implement such a query in TotalPress.

    Theme Author Thomas

    (@tsquez)

    Hi there,

    The way TotalPress is constructed is with hooks and filters. This allows the end user to easily manipulate the way things work in the theme.

    All of these hooks and filters are located in a file called template-functions.php. This file is located in totalpres > assets > inc. A few others are located in a file called totalpress-extras.php. This file is located in the same folder as mentioned before.

    These hooks are called in different theme files. For example, the loop was turned into an action called totalpress_main_loop and this is called in the archive.php and index.php files.

    Another reason why hooks and filters were used to create TotalPress is so that the end user did not have to copy any files into their child theme. All they would have to use is the functions.php file to add their own hooks and filters and to overwrite any existing ones.

    So basically what you need to do is or what I recommend you do create a child theme if you haven’t already done so. Then turn the query above into a hook, like this:

    // my specific author hook
    if ( ! function_exists('build_posts_from_specific_author')) :
    	function build_posts_from_specific_author() { 
    		$args = array(
    		    'author_name' => 'Bedrich',
    		    'post_type' => 'my-custom-post',
    		    'meta_key' => 'color',
    		    'meta_value' => 'blue');
    		$the_query = new WP_Query( $args ); ?>
    		<?php if ( $the_query->have_posts() ) : ?>
    		    <!-- the loop -->
    		    <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    		        <h1 href="<?php the_permalink(); ?>"><?php the_title(); ?></h1>
    		        <?php the_content(); ?>
    		    <?php endwhile; ?>
    		<?php endif; ?>
    	<?php
    	}
    	add_action('posts_from_specific_author','build_posts_from_specific_author');
    endif;

    Add the code to the child theme functions.php file and then call it where you need to like so:

    <?php do_action('posts_from_specific_author'); ?>

    I hope this helps, without knowing the actual specifics of how you’re going to display these posts or where you’re going to display these posts this about all I can offer.

    If possible, you can contact me via ThemeAWESOME.com and I can help you get this sorted if you like.

    Thank you very much!
    I will try it this way.

    Bedrich

    Theme Author Thomas

    (@tsquez)

    Awesome! Please let me know how it goes for you and if there is anything else I can assist you with.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Custom Query’ is closed to new replies.