Archived

This topic is now archived and is closed to further replies.

  • 0

Custom Wp_Query

Question

Posted · Report post

Hi,

 

I would like to do a custom query. I have added a custom query in the postloop section (which I copy-pasted in a child theme):

 

 query_posts(array(              
                              'gdsr_sort' => 'rating',  
                              'paged' => get_query_var('paged')         
                              )  
                        ); 

 

 

It is working. However, pagination is not working properly. I understand from different forums that I should use wp_query. My question is how I can use wp_query within Pagelines. I have tried:

 

$query = new WP_Query( 'gdsr_sort=rating' );

 

but I don't know how I can execute this new query in Pagelines. 

 

Thank you.

Share this post


Link to post
Share on other sites

23 answers to this question

Posted · Report post

Well. The above code does work (incl. pagination) with the GET method! Are there any security issues with using GET? I guess it is fine to use for a filter?

 

Thanks!

Share this post


Link to post
Share on other sites

Posted · Report post

I already was using the Post/Page Pagination section below the new section, hence that's not it. 

 

I understand the pre_get_posts filter is the best solution as it runs (as it's name suggest) before the first query and prevents unnecessary requests, but I don't know how to execute it out of a form action. Any ideas?

 

An alternative could be to use wp_query. Any ideas how I can use a custom wp_query within Pagelines?

Share this post


Link to post
Share on other sites

Posted · Report post

hey all. following simon's directions, i have copied the postloop into my child theme. 

i did that, but i don't think i renamed it correctly.

 

i renamed it to section-2.php

 

the name of the section (at the folder level) is still postloop.

 

so: pagelines-template-theme > sections > postloop > section-2.php

 

this can't be right, can it?

 

and, if the php is named correctly, is it alright to leave the "postloop" folder with the name "postloop"?

 

thank you.

Share this post


Link to post
Share on other sites

Posted · Report post

I tried this:

 

add_action('loop_start','my_form_filter');
function my_form_filter(){
?>
<form action="<?php echo home_url(); ?>" method="post">
   <input class="btn btn-mini" name="filter" type="submit" value="rating">
   <input class="btn btn-mini" name="filter" type="submit" value="date">
</form>
<?php
}

add_action( 'pre_get_posts', 'exclude_category',1 );
function exclude_category( $query ) {
    if(isset($_POST['filter'])) {
    $filter = $_POST['filter']; 
        switch ($filter) {
                
            case 'rating':

                if ( $query->is_main_query() ) {
                    $query->set( 'gdsr_sort', 'rating');
                }


            break;
            
            case 'date':
               if ( $query->is_main_query() ) {
                    $query->set( 'order', 'desc');
                }
             break;
        }
    }
}

 

Stil no luck with pagination....

Share this post


Link to post
Share on other sites

Posted · Report post

Suzy,

 

You don't rename the section.php, but the folder containing it.

 

For example, inside a section folder called boxes there's a file called section.php. The same for every section.  But if you wanted to make a new boxes section, you'd copy the folder, rename it and call it something like boxes2, leaving the section.php inside perfectly intact.

Share this post


Link to post
Share on other sites

Posted · Report post

I just went through all of this as I wrote AnyLoop, a custom post loop section on the store. It has multiple wp_queries and can run simultaneously. And after much frustration, using the section I described works, with wp_query. Feel feel to experiment on your own, I'm just trying to save you some time.

Share this post


Link to post
Share on other sites

Posted · Report post

That is very much appreciated. 

 

How can I execute a wp_query in Pagelines?

Share this post


Link to post
Share on other sites

Posted · Report post

I cannot imagine this is hard for a pagelines developer :) Pretty basic customization stuff I would say. It would be very much appreciated if someone could give an example of how I can do a custom wp_query in Pagelines while using the Pagelines loop. 

Share this post


Link to post
Share on other sites

Posted · Report post

so i had it EXACTLY backward. thank you! do i have to do anything else?

Share this post


Link to post
Share on other sites

Posted · Report post

Just create your own section using the Base Section (known as the free Pull Quote example section in wp admin store), drop in the query, and fashion the output how you please. Then just drop the section on whatever you page you want.

Share this post


Link to post
Share on other sites

Posted · Report post

I don't believe so.

 

But in future, if you see a topic is answered, don't add to it.  We generally ignore these because they're resolved. Open a new topic rather than tagging onto someone else's.  That way you get direct attention.

1 person likes this

Share this post


Link to post
Share on other sites

Posted · Report post

function add_gdsr_rating( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
        $query->set( 'gdsr_sort', 'rating' );
    }
}
add_action( 'pre_get_posts', 'add_gdsr_rating' );

 

That adds your rating, but you need to add a check in there for 'if a button is pressed'

Share this post


Link to post
Share on other sites

Posted · Report post

Do you mean I have to build and style my own loop? I was hoping I could create a custom wp_query and use the Pagelines loop. Is that possible?

Share this post


Link to post
Share on other sites

Posted · Report post

Thanks. I see I was struggeling with this already in december :-)

I have this working with post_query. Created a custom section in the base child theme. This works:

 

<?php
/*
	Section: Filter
	Author: PageLines
	Author URI: http://www.pagelines.com
	Description: The Filter Loop. Includes content and post information.
	Class Name: filterloop	
	Workswith: templates, main
	Failswith: 404_page
*/

class filterloop extends PageLinesSection {

   function section_template() { 

       query_posts(     array(              
                             'order' => 'DESC',
                             'orderby' => 'date'    
                             )  
                         ); 

       $theposts = new PageLinesPosts();
       $theposts->load_loop();

   }
}

 

I can change the array of the query_post to get any custom loop I would like. However, using query_posts gives trouble with the pagination. I read on a couple of forums that I should use wp_query. Let's say I would like to query only cat 3. Wordpress codex shows this code:

$the_query = new WP_Query('cat=3');

// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post();
	echo '<li>';
	the_title();
	echo '</li>';
endwhile;

// Reset Post Data
wp_reset_postdata();

Adding this in a custom section also works great. It creates a list of category 3 post titles. Instead of this simple loop I would like to load the Pagelines loop. How can I do that? I tried 

 

load_loop();

 

but that didn't work.

 

Thanks!

Share this post


Link to post
Share on other sites

Posted · Report post

Hmm, it would appear that the code on the link I gave you has been stripped, let me find this out for you.

Share this post


Link to post
Share on other sites

Posted · Report post

Use WP Query to run a new query.

Share this post


Link to post
Share on other sites

Posted · Report post

@beardedavenger could you give me an example please?

Share this post


Link to post
Share on other sites

Posted · Report post

Hi,

 

Thank you for your time!. Actually I already had the filter working. What I am trying to accomplish is a couple of buttons wihch users can click to sort the posts (based on rating, views and date). I would like to have the filter buttons above all loops and behave dynamic. Hence, when a user is on a category/taconomy/tag page it will filter the posts within that taxonomy. My guess was that I should use a custom wp_query instead of the pre_get_post filter to allow dynamic behaviour? That is why I was looking for an example of a wp_query instead of query_posts within pagelines?

 

In the meantime I could show the buttons only for the mainloop on the homepage. The code below works, but not with pagination... :-) Maybe because the pre_get_posts filter gets the $filter variable to late or something? 

 

add_action('loop_start','my_form_filter');
function my_form_filter(){
?>
<form action="<?php echo home_url(); ?>" method="post">
   <input class="btn btn-mini" name="filter" type="submit" value="rating">
   <input class="btn btn-mini" name="filter" type="submit" value="date">
</form>
<?php
}

add_action( 'pre_get_posts', 'exclude_category',1 );
function exclude_category( $query ) {
    if(isset($_POST['filter'])) {
    $filter = $_POST['filter']; 
        switch ($filter) {
                
            case 'rating':

                if ( $query->is_main_query() ) {
                    $query->set( 'gdsr_sort', 'rating');
                }


            break;
            
            case 'date':
               if ( $query->is_main_query() ) {
                    $query->set( 'order', 'desc');
                }
             break;
        }
    }
}

Share this post


Link to post
Share on other sites

Posted · Report post

Hi, I think the best course of action would be to contact one of our Pros regarding this customisation. http://www.pagelines.com/pros/

Share this post


Link to post
Share on other sites

Posted · Report post

Google search pre_get_posts filter

1 person likes this

Share this post


Link to post
Share on other sites

Posted · Report post

I am not sure, but I think I cannot use this filter as I want to make a frontend filter for users (i.e. a buttons to sort by rating, date etc). Or is it possible to execute the filter only after a button is submitted?

Share this post


Link to post
Share on other sites

Posted · Report post

You don't need a filter. Paged results is kind of a bitch to get working correctly within PageLines. There's a default section called Pagination that will work with your query in your section. Just drag the Post/Page Pagination section below your new section.

Share this post


Link to post
Share on other sites
This topic is now closed to further replies.