• Resolved radgh

    (@radgh)


    Let’s say I have 5 articles within a custom post type named “Games”:

    Minecraft, Terraria, Amnesia, Braid, Battlefield

    If the user requests the page “Amnesia”, I need to figure out the index of this page (in this example, the index would be 3).

    I also need the opposite, if the user requests item #3, I need to send them “Amnesia”.

    I am currently using a custom wp_query to start pagination:

    $args = array(
        'post_type'	      => 'press',
        'posts_per_page'  => 5,
        'paged'           => $paged,
        'orderby'         => 'date',
        'order'           => 'DESC',
    );
    $nav_query = new WP_Query( $args );

    In the above code, the $paged variable is not set – nor will it ever be set. I need to replace $paged with something like:

    floor( $the_page_index / $posts_per_page )

Viewing 5 replies - 1 through 5 (of 5 total)
  • Are you talking about the database index or do you mean something like “the paginated page that the post will be on”?

    You pull the post by ‘name’ or ‘pagename’ to get the database index and to “send” the page, if I understand you and I am not sure I do.

    Thread Starter radgh

    (@radgh)

    “the paginated page that the post will be on” is exactly what I meant.

    I think I am just going outside of what wordpress is made for, perhaps this isn’t the best way to lay it out to begin with.

    For now I’ve got a pretty wasteful way of doing it. I get the ID of the active page, do a custom wp_query to get all posts, loop through them with a counter and when I find the active link, I return the counter.

    Now with the counter I can do some math to find out what page it’s on. Since I have a full database stored I can also grab the next/prev/numbered pagination links too – since they won’t simply point to “press/page/2” but instead “press/article-name” directly.

    If this were a large website this would be very hacky, but for now I’m ok with it.

    But yeah, if there is a way to do this without pulling the entire custom post database to each request, I’d sure love to know. But I think that’s one of the features that is purposefully abstracted, as it would make things too complicated and there are better ways.

    What you are trying to do isn’t easy. Maybe something like this:

    https://www.leftjoin.net/2011/05/mysql-getting-row-position-in-result-set/

    Thread Starter radgh

    (@radgh)

    I never thought about using MySQL to do this. I guess that’s the best way, my current implementation is wasting several mysql requests and creating multiple wp_query objects. I could certainly do all of the heavy lifting with one (maybe two?) mysql queries. I did read somewhere not to worry about system resources for small scale projects (this website will have <50 visitors per day) but I can’t help but feel like it will take noticeably longer to load than the others.

    I’ll look into this to optimize my script later. I’m not as comfortable with MySQL so that will be a journey of its own. Thanks though, I think I have what I need now. This link will be a great help, too.

    MySQL may or may not be the best way. Sometimes PHP can do the job more quickly, sometimes not. There are a lot of factors involved.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Get the index (or position) of a post within a custom post type?’ is closed to new replies.