Support » Fixing WordPress » Problem with merging custom posts

  • Hello!

    Yesterday i found a code to merge my two custom posts into a new custom post. Everything worked great and everything looks fine!

    However, there is one problem.. My pagination that is showing all my custom posts doesn’t work anymore. Also latest posts doesn’t show anything. However if i add a new post in my custom post it gets added to the recent posts.

    Does anyone have a clue why my pagination and recent posts doesn’t work anymore? It shows 1/3500 pages but page cannot be found when entering a page.

    This is the code i used to merge my custom posts:

    global $wpdb;
    $wpdb->query("UPDATE $wpdb->posts SET post_type = 'Recipes' WHERE post_type IN('food-recipes','drink-recipes')");
    update_option('updated_my_books', true);
Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    How did you add your post types? Did you also update post type registrations through that same mechanism?

    Please visit your permalinks settings screen. Don’t change anything, just load the screen. This causes the rewrite rules to be flushed and recompiled. Also flush any caching plugins, plus your own browser’s cache to ensure you are working with current data.

    If you are still having trouble after that, please provide a link to a page showing pagination links that don’t work. Also a link for the recent posts content if it’s not the same page. What pagination function is showing the bad links? What are you using to show recent posts? A widget? Which one?

    Hi @bcworkz the permalinks flush i’ve already tried and doesn’t work. I cant post the website here but i can send u in pm on slack!

    This is the code when i created my custom post.

    function my_custom_posttypes() {
    
        // Recipes
        $labels = array(
            'name'               => 'Recipes',
            'singular_name'      => 'Recipes',
            'menu_name'          => 'Recipes',
            'name_admin_bar'     => 'Recipes',
            'add_new'            => 'Add New',
            'add_new_item'       => 'Add New Recipes',
            'new_item'           => 'New Recipes',
            'edit_item'          => 'Edit Recipes',
            'view_item'          => 'View Recipes',
            'all_items'          => 'All Recipes',
            'search_items'       => 'Search Recipes',
            'parent_item_colon'  => 'Parent Recipes:',
            'not_found'          => 'No Recipes found.',
            'not_found_in_trash' => 'No Recipes found in Trash.',
        );
        
        $args = array(
            'labels'             => $labels,
            'public'             => true,
            'publicly_queryable' => true,
            'show_ui'            => true,
            'show_in_menu'       => true,
            'query_var'          => true,
            'rewrite'            => array( 'slug' => 'recipes' ),
            'capability_type'    => 'post',
            'has_archive'        => false,
            'hierarchical'       => false,
            'menu_position'      => 5,
            'menu_icon'          => 'dashicons-megaphone',
            'supports'           => array( 'title', 'editor', 'thumbnail', 'excerpt' ),
            'taxonomies'         => array( 'category', 'post_tag' )
        );
        register_post_type( 'recipes', $args );
    }
    
    add_action('init', 'my_custom_posttypes');
    
    // Flush rewrite rules to add "post name" as a permalink slug
    function my_rewrite_flush() {
        my_custom_posttypes();
        flush_rewrite_rules();
    }
    register_activation_hook( __FILE__, 'my_rewrite_flush' );

    Code for my template :

    <?php 
    
      $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
    
      $custom_args = array(
          'post_type' => 'recipes',
          'posts_per_page' => 12,
          'paged' => $paged
        );
    
      $custom_query = new WP_Query( $custom_args ); 
      
      $current_date = date("Y-m-d");
    ?>
    
    <?php if ( $custom_query->have_posts() ) : ?>
    
      <!-- the loop -->
      <?php while ( $custom_query->have_posts() ) : $custom_query->the_post(); ?>
    
      
      <div class="sbo_news_article">
                <a href="<?php the_permalink(); ?>">
                <?php
                if ( $posted_date == get_the_time('H:i') ) {
                  echo"<div><span class=\"sbo_news_date\">I dag $posted_date</span><span class=\"sbo_news_source\">$source</span></div>";
                } else {
                  echo "<div><span class=\"sbo_news_date\">$posted_date</span><span class=\"sbo_news_source\">$source</span></div>";
                }
                ?>
                  <h2 class="sbo_news_title"><?php the_title(); ?></h2></a>
                </div>
    
        <?php endwhile; ?>
    
        <!-- end of the loop -->
    
        <!-- pagination here -->
        <?php
          if (function_exists(custom_pagination)) {
            custom_pagination($custom_query->max_num_pages,"",$paged);
          }
        ?>
    
      <?php wp_reset_postdata(); ?>
    
      <?php else:  ?>
        <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
      <?php endif; ?>

    Code in functions:

    function custom_pagination($numpages = '', $pagerange = '', $paged='') {
    
      if (empty($pagerange)) {
        $pagerange = 2;
      }
    
      global $paged;
      if (empty($paged)) {
        $paged = 1;
      }
      if ($numpages == '') {
        global $wp_query;
        $numpages = $wp_query->max_num_pages;
        if(!$numpages) {
            $numpages = 1;
        }
      }
    
      $pagination_args = array(
        'base'            => get_pagenum_link(1) . '%_%',
        'format'          => 'page/%#%',
        'total'           => $numpages,
        'current'         => $paged,
        'show_all'        => False,
        'end_size'        => 1,
        'mid_size'        => $pagerange,
        'prev_next'       => False,
        'prev_text'       => __('&laquo;'),
        'next_text'       => __('&raquo;'),
        'type'            => 'plain',
        'add_args'        => false,
        'add_fragment'    => ''
      );
    
      $paginate_links = paginate_links($pagination_args);
    
      if ($paginate_links) {
        echo "<nav class='custom-pagination'>";
          echo "<span class='page-numbers page-num'>Sida " . $paged . " av " . $numpages . "</span> ";
          echo $paginate_links;
        echo "</nav>";
      }
    
    }
    Moderator bcworkz

    (@bcworkz)

    Thanks for all the information, as well as the link. A Slack PM is fine for information you do not want to be public. Let’s be sure to keep all support here in the forums though. Just as you have done. You seem to understand this, I just wanted it to be clear for anyone else following along.

    What has happened to you is mixing pagination parameters of the default query with query parameters of your custom query. While it seems like a perfectly logical approach, I’ve never seen it work reliably. There may be a way, but I don’t know what it is because there are better ways. You may say “but it worked before the change!”. I didn’t say it wouldn’t work, I said it’s unreliable ๐Ÿ™‚

    One thing you can do is collect your own dedicated page parameter instead of relying upon $paged. Also, do not assume global $wp_query contains your query. For consistency, you need to pass your query ($custom_query) to your pagination function for its use.

    IMO, an even better way is to not make a custom query on a template at all. Instead, alter the main, default query through the “pre_get_posts” action. Your callback here typically first checks that the passed query is not for admin and that it’s the main query for the request. Then be sure the query is the one you want to alter and not some other main non-admin query. There is invariably some unique combination of query vars that indicate the correct query. Perhaps the post_type == ‘recipes’ is enough.

    When you alter the main query, the default pagination used by your theme is often adequate, so there may be no need for a custom pagination function.

    Thanks for your information! Today however it magically just started to work again without me changing anything.. Iยดm very new to WordPress and i dont understand like anything of what you wrote above but hopefully i will in the near future! ๐Ÿ™‚

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.