kylephillips / favorites

Simple and flexible favorite buttons for any WordPress post type.
https://favoriteposts.com
223 stars 86 forks source link

where is the query that gets favorite posts ? is there any way to add pagination ? #109

Open m-logicano opened 5 years ago

m-logicano commented 5 years ago

in the beginning i want to thank you for the great plugin , actually, i was trying to add pagination to the favorites plugin by connecting it with another pagination plugin but i cant reach the variable that does the query which gets the favorite posts from the database . iam searching for a variable that contains code like "wp_query" or so ican make pagination or limit posts that appears . i have searched all the files but i found nothing

gfargo commented 5 years ago

Hey @logicano, are you familiar with using WordPress filters?

I haven't had time to personally test this out but there's a filter inside of the UserRepository.php class that allows you to filter the # of favorites returned.

https://github.com/kylephillips/favorites/blob/master/app/Entities/User/UserRepository.php#L80

favorites/user/favorites

Seems like you would either have to implement some kind of custom pagination within your template OR follow a similar path that @ben-heath mentioned in https://github.com/kylephillips/favorites/issues/50#issuecomment-273232052 where you retrieve all of the favorite post ID's and then pass them into a custom WP_Query loop. In there you can specify # of posts per page and provide "next" and "back" links like in his code example.

<?php
$favorites = get_user_favorites();

<div class="container">

    <?php if ( empty ( $favorites ) ) : ?>
        <!-- No Favorites Found -->
        <h3 id="no-favs-yet">No Favorites Yet</h3>
    <p>Click on a heart to save a wedding as one of your favorites.<br>Reload the page to see your updated favorites.</p>
    <?php else : ?>

    <?php
        // Retrieve current pagination link
        $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;

        $favorites_query = new WP_Query( [
            'post_type' => [ 'my-post-type' ], // Post type being displayed.
            'posts_per_page' => 10, // Control how many favorites on each page we want.
            'ignore_sticky_posts' => true,
            'post__in' => $favorites,
            'paged' => $paged // If you want to include pagination, and have a specific posts_per_page set
        ] );

        // Loop over favorite posts
        while ( $favorites_query->have_posts() ) : 
            $favorites_query->the_post();

            // Markup for each favorite below.
            ?>              
            <div class="grid-post_outer masonry-item masonry-brick">
                <div class="postformat-standard">
                    <a class="blog-thumb" href="<?php the_permalink(); ?>">
                        <h3><?php the_title(); the_favorites_button(); ?></h3>
                    </a>
                    <a href="<?php the_permalink(); ?>">
                        <?php the_post_thumbnail(); ?> 
                    </a>
                </div>
            </div>
    <?php endwhile; ?>
    <?php
        // Link to goto next page of favorites.
        next_posts_link( 'Older Favorites', $favorites_query->max_num_pages );
        // Link to goto previous page.
        previous_posts_link( 'Newer Favorites' );
    ?>
    <?php endif; ?>
    <?php wp_reset_postdata(); ?>
</div>
m-logicano commented 5 years ago

Really , I cant thank you enough you really helped me . i used $favorites = get_user_favorites();

but i still curious about where how this plugin gets posts from Database where is the wp_query ? i Even cant find any function that gets the saved settings from the DB

gfargo commented 5 years ago

Hey @logicano,

Most of the queries to grab the favorite information is stored on post meta or is retrieved by manually creating a DB query e.g. all favorite counts are retrieved in the FavoritesCount.php class which is then pulled into the PostFavorites.php class here.

The saved settings created by the user inside WP Admin are retrieved by the SettingsRepository.php class.

ghost commented 5 years ago

Hey @GFargo,

Do you have any idea why the ajax updating stops working when custom WP_Query loop is used?

gfargo commented 5 years ago

Hey @altjonK, not sure I follow. Do you have this documented in another issue?

zrpgood commented 5 years ago

@GFargo Hello, can the article's favorite list be sorted according to the time of the collection operation?And can I turn the page to display numbers? Not just new and old.I looking forward to your reply, thank you very much.

gfargo commented 5 years ago

@zrpgood Given the approach above, if you wanted to add numbered pagination to your page you'd want to use the paginate_links function. https://codex.wordpress.org/Function_Reference/paginate_links

It will output both the links to next/prev as well as the number of pages in between.

For sorting, have you checked out the orderby param for WP_Query? https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters

hellohana commented 3 years ago

Hi @GFargo, first of all thank you for providing the solution. However I bumped into a problem. The list that shows all the favorites of a particular user doesnt seem to update with this query. is there anyway to fix this? I know that this is an old thread, but I was kinda hoping someone is facing the same problem? I figured is the ajax not working for some reason.

DivvyPT commented 1 year ago

Hello @GFargo, where should I add your code to add a pagination in the favorite list page? Please help me, I really need this in my site :) Thank you in advance!

gfargo commented 1 year ago

The list that shows all the favorites of a particular user doesnt seem to update with this query.

Hey @hellohana, sorry for the abysmally slow reply, have been distracted in typescript land lately. When are you expecting the query to update? Because this is PHP, this query will only be run when the user views the page.

If you want it to update after the user clicks something to "favorite" you'd likely need to write some logic in JS to query the WordPress API for the same data and update the DOM with the new results.

gfargo commented 1 year ago

where should I add your code to add a pagination in the favorite list page?

Wonder if everyone would benefit from wrapping up the pagination functionality into a shortcode? The shortcode could accept various params to control the output.

Admittedly, it's been awhile since I've looked/contributed to this lib, so maybe a better route would be adding a param to an existing shortcode to toggle pagination instead 💭